0

NSDictionary のすべての値を効率的かつ簡単に合計する方法を探しています。

たとえば、次のような NSDictionary があるとします。

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

これを試してみてください...これが役立つかもしれません..

-(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 に答える
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 に答える