0

我有以下核心数据结构: 核心数据

我正在尝试将 Vocabulary 对象添加到 Group 类。

我尝试使用 [Group addObject: VocabularyObject] 方法执行此操作无济于事。

 AppDelegate *delegate = [[AppDelegate alloc]init];

 Group *group = [_arr objectAtIndex:indexPath.row]; //I have an array with 'Group' objects

    //create vocabulary item
    Vocabulary *vocabularyEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:[delegate managedObjectContext]];
    vocabularyEntity.prompt = @"Here is a cool prompt";
    vocabularyEntity.definition = @"Here is an even cooler definition";

    [delegate saveContext];

    [group addTermsObject:vocabularyEntity];

我收到此错误,我使用了异常断点,错误来自 addTermsObject 调用。

[__NSDictionaryI addTermsObject:]: unrecognized selector sent to instance 0x74c4f70

我要添加的对象绝对是一个词汇对象,所以我不确定问题可能是什么。

有任何想法吗?

谢谢!

4

1 回答 1

1

错误消息指出您的

Group *group = [_arr objectAtIndex:indexPath.row];

不是您预期托管对象,而是NSDictionary. 也许您使用

[fetchRequest setResultType:NSDictionaryResultType];

在获取请求中?在这种情况下,所有获取的对象都只是字典,与托管对象上下文没有任何联系,您不能使用这些字典建立任何关系。

更新:另一个错误在这里:

AppDelegate *delegate = [[AppDelegate alloc]init];

这会分配一个的应用程序委托,而不是使用现有的。这可能不是您想要的,您应该将其替换为

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
于 2013-06-18T18:11:56.243 回答