Marko Zadravec
Marko Zadravec

Reputation: 8738

Sum of values in NSDictionary

I am looking for the efficient and easy way to sum all of the values in NSDictionary.

For example, if I have NSDictionarys like:

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

I would like to get a NSDictyonary

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

Upvotes: 0

Views: 1358

Answers (3)

Rana
Rana

Reputation: 451

Try with this... may be this will help you..

-(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];
}

Upvotes: 0

user1673099
user1673099

Reputation: 3289

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

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

Upvotes: 3

Kumar KL
Kumar KL

Reputation: 15335

Try with this one :

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

                                                     };

Upvotes: 0

Related Questions