1

編集:より良い例...

したがって、「既存の」オブジェクトの 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 の新しいオブジェクトがセットに追加されます。

@"id" プロパティが存在することを確認するためだけに、すべての新しいオブジェクトに対して NSMutableSet をループすることを本当に避けたいです... addObjectsFromArray を使用してこれらの新しいオブジェクトをマージできると考えていましたが、プロパティが異なるため (例:スコア: 9999) 新しいオブジェクトがセット内の既存のオブジェクトとして認識されていません。

この例を単純化するために、数値を文字列として使用しています。また、アプリは 3.0 と互換性があるため、iOS SDK 4.0 のみの機能の使用は避けたいと思います。

ありがとうございます!それは有り難いです!

4

3 に答える 3

4

あなたの質問からは完全にはわかりませんが (iphone の質問なので、目的の c 表記を使用する必要がありました)、NSDictionary があなたの親友になる可能性があるようです。

于 2010-10-28T18:23:47.037 に答える
0
NSArray *array = ... // contains the new dictionary objects.
NSMutableSet *set = ... // contains the existing set of dictionary objects. 

for (id arrayItem in array)
{
    id setItem = [[set filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"id.intValue = %d", [[arrayItem valueForKey:@"id"] intValue]]] anyObject];
    if (setItem != nil)
    {
        [set removeObject:setItem];
        [set addObject:arrayItem];
    }
}
于 2010-10-28T19:13:19.560 に答える
0

最善の策は、@"id" パラメータを使用して最初のセットを辞書に変換することです。これは、あなたが望むことを行うべきコードであり、 @"id" パラメーターを持たない辞書を持つ場合も処理します (その時点でそれらはマージされず、結果に含まれるだけです):

// here is the NSSet you already have
self.objects = [NSSet setWithObjects:
    [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];

// 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];

// Convert self.objects into a dictionary for merging purposes
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:[self.objects count]];
NSMutableSet *remainder = [NSMutableSet set]; // for objects with no @"id"
for (NSDictionary *obj in self.objects) {
    NSString *objId = [obj objectForKey:@"id"];
    if (objId) {
        [dict setObject:obj forKey:objId];
    } else {
        [remainder addObject:obj];
    }
}

// merge the new objects in
for (NSDictionary *obj in newObjects) {
    NSString *objId = [obj objectForKey:@"id"];
    if (objId) {
        // merge the new dict with the old
        // if you don't want to merge the dict and just replace,
        // simply comment out the next few lines
        NSDictionary *oldObj = [dict objectForKey:objId];
        if (oldObj) {
            NSMutableDictionary *newObj = [NSMutableDictionary dictionaryWithDictionary:oldObj];
            [newObj addEntriesFromDictionary:obj];
            obj = newObj;
        }
        // stop commenting here if you don't want the merging
        [dict setObject:newObj forKey:objId];
    } else {
        [remainder addObject:obj];
    }
}

// add the merged dicts back to the set
[remainder addObjectsFromArray:[dict allValues]];
self.objects = remainder;
于 2010-10-28T22:49:27.867 に答える