不能为对象创建NSSet错误



我是CoreData的新手,所以我写了一个简单的程序来了解更多关于交互和事情如何工作-但我一直得到一个错误。我有两个实体,"父"one_answers"子"具有多对多关系(一个父可以有许多孩子)。

基本上,当我添加一个新的子元素时,我需要关联它,add, Parent。如果我用Parent实体预先填充一个ComboBox,就没有问题了,一切都按计划进行。但是,除非我放置两个组合框,否则我只能添加一个父级。因此,我需要能够获取Parent,然后添加,这就是错误的来源。

代码:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc}init];
NSEntityDescription *entity = [[NSEntityDdescription entityForName:@"Parent" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormate:@"firstName like[cd] %@", nameValue];
[fetchRequest setPredicate: predicate];
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSManagedObject aParent = [results objectAtIndex:0];
NSLog(@"First name: %@", [aParent valueForKey:@"firstName"]);  //This does yield the correct information in NSLog
NSManagedObject *newChild = [.....]; 
[newChild setValue:name forKey:@"firstName"];
//No error to this point, but when I do the next line I receive the error
[newChild setValue:aParent forKey:@"parentName"];

我试过抓取NSSet, nsmutableet等…我几乎读了我能在网上找到的所有苹果CoreData书,但都没有用。

这个问题我已经纠结了好几天了,如果有什么建议我将不胜感激。

结果证明我的代码是正确的。在我的CoreData设置中,我忘记了一个次要/主要的细节,我没有将关系设置为多对多。我以为我有,但是我想我应该再检查一遍。

无论如何,我上面的代码在我改变关系后工作得很好。

最新更新