私はこれを何度も繰り返してきましたが、setValue:forKey:を介して関係を割り当てることが機能しない理由を一生理解することはできません。私の方法は以下の通りです。
-(void)saveNewCartItemName:(NSString *)name Price:(float)price Qty:(int)qty Tax:(int)tax inCart:(NSDate *)cartDate{
NSManagedObject *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item"
inManagedObjectContext:self.appDelegate.managedObjectContext];
NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Cart"];
NSLog(@"cartDate = %@", cartDate);
[query setPredicate:[NSPredicate predicateWithFormat:@"cartDate = %@", cartDate]];
NSError *error;
NSMutableArray *queryResults = [[self.appDelegate.managedObjectContext executeFetchRequest:query error:&error] mutableCopy];
NSLog(@"queryResults count = %d", [queryResults count]);
if ([queryResults count] == 1) [newItem setValue:[queryResults objectAtIndex:0] forKey:@"cartThatHasItem"];
[newItem setValue:name forKey:@"itemName"];
[newItem setValue:[NSNumber numberWithFloat:price] forKey:@"itemPrice"];
[newItem setValue:[NSNumber numberWithInt:qty] forKey:@"itemQty"];
[newItem setValue:[NSNumber numberWithInt:tax] forKey:@"itemTax"];
[self.appDelegate.managedObjectContext save:&error];
}
毎回クラッシュする行は次のとおりです。
if ([queryResults count] == 1) [newItem setValue:[queryResults objectAtIndex:0] forKey:@"cartThatHasItem"];
My NSLog shows that there is a NSManagedObject to assign the relationship to, and I have double checked the name in the forKey: that there are no typos. All that Xcode tells me is that it prints out '(null)' on my console and then tells me 'libc++abi.dylib: terminate called throwing an exception' on the next line.
Why is it crashing? And how do I fix it?
Update: That's the entire error message.
2012-07-03 18:28:57.844 appName[3063:11603] (null)
libc++abi.dylib: terminate called throwing an exception
(lldb)