我想将记录保存在一个数组中并从 CoreData 中删除它们。我曾尝试使用 NSCopying,但似乎 copyWithZone 不适用于 NSManagedObject。我真的被卡住了,任何帮助将不胜感激。
问问题
189 次
1 回答
1
将 NSManagedObject 的所有字段值复制到字典,并将这些字典存储在数组中。
我已经整理了一个小例程,您可以在 NSMutableObject 类别中实现它,该类别可用于获得它。
(请注意,我现在不在 Mac 电脑上,代码可能有拼写错误,但除此之外,它应该可以正常工作)。
-(NSDictionary*)retrieveAsDict {
NSMutableDictionary *aux = [[NSMutableDictionary alloc] init];
NSDictionary *attributes = [self attributesByName];
NSArray *attributeNames = [attributes allKeys];
for (NSString *key in attributeNames) {
[aux setValue:[self valueForKey:key] forKey:key];
}
NSDictionary *ret = [NSDictionary dictionaryWithDictionary:aux];
// Uncomment this if not using ARC
//[aux release];
return(ret);
}
如果您需要进一步澄清,请务必发表评论,我会尽快回复。
于 2011-11-16T13:49:37.257 回答