0

我正在寻找一种有效且简单的方法来总结 NSDictionary 中的所有值。

例如,如果我有 NSDictionarys,例如:

NSDictionary * first = @{@"A": [NSNumber numberWithInt:1],
                         @"B": [NSNumber numberWithInt:2],
                         @"C": [NSNumber numberWithInt:3],
                         @"D": [NSNumber numberWithInt:4]};

NSDictionary * second = @{@"A": [NSNumber numberWithInt:1],
                         @"B": [NSNumber numberWithInt:2],
                         @"C": [NSNumber numberWithInt:3],
                         @"D": [NSNumber numberWithInt:4]};

我想获得一个NSDictyonary

NSDictionary * sum = @{@"A": [NSNumber numberWithInt:2],
                         @"B": [NSNumber numberWithInt:4],
                         @"C": [NSNumber numberWithInt:6],
                         @"D": [NSNumber numberWithInt:8]};
4

3 回答 3

3
NSDictionary * first = @{@"A": [NSNumber numberWithInt:1],
                         @"B": [NSNumber numberWithInt:2],
                         @"C": [NSNumber numberWithInt:3],
                         @"D": [NSNumber numberWithInt:4]};

NSDictionary * second = @{@"A": [NSNumber numberWithInt:1],
                          @"B": [NSNumber numberWithInt:2],
                          @"C": [NSNumber numberWithInt:3],
                          @"D": [NSNumber numberWithInt:4]};
NSMutableDictionary *Third=[[NSMutableDictionary alloc]init];
for (NSString* key in [first allKeys]) {
    NSLog(@"%@",key);

    int a=[[first objectForKey:key]integerValue];
    int b=[[second objectForKey:key]integerValue];

    int c= a+b;

    [Third setObject:[NSNumber numberWithInt:c]  forKey:key];

    }

NSLog(@"%@",Third);

更新了不同的键

NSArray *Key_of_First=[first allKeys];
NSArray *Key_of_Second=[second allKeys];

for (int j=0; j<Key_of_First.count; j++) {

    int a=[[first objectForKey:[Key_of_First objectAtIndex:j]]integerValue];
    int b=[[second objectForKey:[Key_of_Second objectAtIndex:j]]integerValue];

    int c= a+b;

    [Third setObject:[NSNumber numberWithInt:c]  forKey:@"Your Key Name which you give"];

}

  NSLog(@"%@",Third);
于 2013-10-23T06:46:34.647 回答
0

试试这个:

 NSDictionary * sum = @{@"A": [NSNumber numberWithInt:
                                  ([[first objectForKey:@"A"] intValue]+
                                   [[second objectForKey:@"A"] intValue])],
                           @"B": [NSNumber numberWithInt:
                                  ([[first objectForKey:@"B"] intValue]+
                                   [[second objectForKey:@"B"] intValue])]

                                                     };
于 2013-10-23T06:46:46.073 回答
0

试试这个......也许这会帮助你..

-(NSMutableDictionary *)sum :(NSDictionary *)first :(NSDictionary *)second
{

    NSMutableDictionary * sum =[[NSMutableDictionary alloc]
                                init];
    NSArray *keysOfFirst =[first allKeys];
    for (id key in keysOfFirst)
    {
        NSNumber * num =[NSNumber numberWithInt:([[first objectForKey:key] intValue] + [[first objectForKey:key] intValue])] ;
        [sum setObject:num forKey:key];
    }

    return [sum autorelease];
}
于 2013-10-23T07:16:46.877 回答