NSDictionary中的值总和

时间:2013-10-23 06:29:47

标签: ios xcode sum nsdictionary

我正在寻找有效且简单的方法来总结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]};

3 个答案:

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

针对Distinct Key进行了更新

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);

答案 1 :(得分: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])]

                                                     };

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