即使从CoreData中删除CoreData结果数组,也要保存它

时间:2011-11-16 13:15:50

标签: iphone objective-c ios core-data nscopying

我想将记录保存在数组中并从CoreData中删除它们。我尝试过使用NSCopying,但似乎copyWithZone在NSManagedObject上不起作用。我真的被困了,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分: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);
}

如果您需要进一步澄清,请务必发表评论,我会尽快回答。