编辑:更好的例子......
所以我有一个“现有”对象的 NSMutableSet,我想下载 JSON 数据,解析它,并将这些新对象与我现有的对象合并,用新下载的对象更新任何重复的对象。以下是现有对象集的样子:
NSArray *savedObjects = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", @"hello there", @"body", @"200", @"score", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"2", @"id", @"hey now", @"body", @"10", @"score", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"3", @"id", @"welcome!", @"body", @"123", @"score", nil],
nil
];
self.objects = [NSMutableSet setWithArray:savedObjects];
// after downloading and parsing JSON... I have an example array of objects like this:
NSArray *newObjects = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", @"hello there", @"body", @"9999", @"score", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"4", @"id", @"what's new", @"body", @"22", @"score", nil],
nil
];
因此,例如,在合并这个新对象后,id 为 1 的对象现在的得分为 9999,并且一个新的 id 为 4 的对象将被添加到集合中。
我真的想避免为每个新对象循环遍历 NSMutableSet 只是为了检查 @"id" 属性是否存在......我在想我可以使用 addObjectsFromArray 来合并这些新对象,但看起来因为属性不同(例如:得分:9999)它没有将新对象视为集合中的现有对象。
我使用数字作为字符串来简化这个例子。我还想避免使用仅限 iOS SDK 4.0 的功能,因为该应用程序将与 3.0 兼容。
万分感谢!我很感激!