Objective c 添加具有相同键的不同NSMutableDictionary的值

Objective c 添加具有相同键的不同NSMutableDictionary的值,objective-c,nsdictionary,xcode8,nsmutabledictionary,Objective C,Nsdictionary,Xcode8,Nsmutabledictionary,我使用的是Xcode 8.2,OSX而不是iOS,Objective-C 我有几种不同的NSMutableDictionary,如: NSMutableDictionary* dict1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:1],@"key1", [NSNumb

我使用的是Xcode 8.2,OSX而不是iOS,Objective-C

我有几种不同的NSMutableDictionary,如:

NSMutableDictionary* dict1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                             [NSNumber numberWithInt:1],@"key1",
                             [NSNumber numberWithInt:14],@"key2",
                             nil];

NSMutableDictionary* dict2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                             [NSNumber numberWithInt:9],@"key1",
                             [NSNumber numberWithInt:1],@"key2",
                             [NSNumber numberWithInt:99],@"key3",
                             nil];

// and many more
我需要以添加匹配键的值的方式组合它们,这样我们就可以得到一个新的NSMutableDictionary(或结果为unmutable),如上面的示例所示:

NSMutableDictionary* result = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                             [NSNumber numberWithInt:10],@"key1", // 9+1
                             [NSNumber numberWithInt:15],@"key2", // 14+1
                             [NSNumber numberWithInt:99],@"key3", // stays the same
                             nil];

下面是一个通用的合并词典,它使用块来解决冲突

NSDictionary *mergeDictionaries( NSDictionary *left,
                                NSDictionary *right,
                                id (^resolveConflict)(id duplicateKey, id leftValue, id rightValue)
                                ) {

    NSArray *sharedKeys = [[left allKeys] filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"%@ CONTAINS SELF", [right allKeys]]];

    NSMutableArray *leftOnlyKeys = [[left allKeys] mutableCopy];
    [leftOnlyKeys removeObjectsInArray: [right allKeys]];

    NSMutableArray *rightOnlyKeys = [[right allKeys] mutableCopy];
    [rightOnlyKeys removeObjectsInArray: [left allKeys]];

    NSMutableDictionary *merged = [[NSMutableDictionary alloc] init];

    [leftOnlyKeys enumerateObjectsUsingBlock:^(id  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
        [merged setObject: left[key] forKey: key];
    }];

    [rightOnlyKeys enumerateObjectsUsingBlock:^(id  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
        [merged setObject: right[key] forKey: key];
    }];

    if(resolveConflict) {
        [sharedKeys enumerateObjectsUsingBlock:^(id  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
            id mergedValue = resolveConflict(key, left[key], right[key]);
            if(nil != mergedValue) {
                [merged setObject: mergedValue forKey: key];
            }
        }];
    }

    return merged;
}
然后

屈服

结果:{ 键1=10; 键2=15; key3=99;}

给你:

NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
for (NSDictionary *dictionary in @[dict1, dict2]) {
    // enumerate the keys and objects in the dictionary
    [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *dictionaryNumber, BOOL *stop){
        // check if the key exists in result
        NSNumber *resultNumber = result[key];
        if (resultNumber)
            // the key exists in result, add the number to the existing number
            result[key] = [NSNumber numberWithInteger:resultNumber.integerValue + dictionaryNumber.integerValue];
        else
            // the key didn't exist in result, add the key and the number to result
            result[key] = dictionaryNumber;
    }];
}

到目前为止,您尝试了什么?遍历每个dicts allkey并比较它们。但一定有更有效的方法来做到这一点!不知道enumerateKeysAndObjectsUsingBlock。这看起来很有趣,似乎比迭代和比较快得多。非常感谢。
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
for (NSDictionary *dictionary in @[dict1, dict2]) {
    // enumerate the keys and objects in the dictionary
    [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *dictionaryNumber, BOOL *stop){
        // check if the key exists in result
        NSNumber *resultNumber = result[key];
        if (resultNumber)
            // the key exists in result, add the number to the existing number
            result[key] = [NSNumber numberWithInteger:resultNumber.integerValue + dictionaryNumber.integerValue];
        else
            // the key didn't exist in result, add the key and the number to result
            result[key] = dictionaryNumber;
    }];
}