我希望能够支持显示核心数据实体的 tableview 行的复制和粘贴。这个实体有一个属性和两个关系。当我使用 Apple 推荐的字典归档技术(来自“NSPersistentDocument 核心数据教程”)时,我发现关系会引发错误。这是发生问题的基本代码:
for (id sectionObject in selectedSectionsArray){
NSDictionary *thisDictionary = [sectionObject dictionaryRepresentation]; // 'sectionObject' has 1 attribute and 2 relationships (one-to-many)
[copyObjectsArray addObject:[sectionObject dictionaryRepresentation]];
}
NSPasteboard *generalPasteboard = [NSPasteboard generalPasteboard];
[generalPasteboard declareTypes:[NSArray arrayWithObjects:MSSectionsPBoardType, NSStringPboardType, nil] owner:self];
NSData *copyData = [NSKeyedArchiver archivedDataWithRootObject:copyObjectsArray]; // Here's where it crashes. ERROR MESSAGE: "-[NSManagedObject encodeWithCoder:] unrecognized selector sent to instance 0x22fd410"
因此,似乎将关系复制到粘贴板的唯一方法必须是归档其 URI。在这种情况下,我必须处理引用临时 ID 的麻烦。有人可以确认是这种情况吗?有必要这么难吗?