Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSDictionary中的值之和_Ios_Xcode_Sum_Nsdictionary - Fatal编程技术网

Ios NSDictionary中的值之和

Ios NSDictionary中的值之和,ios,xcode,sum,nsdictionary,Ios,Xcode,Sum,Nsdictionary,我正在寻找一种有效且简单的方法来求NSDictionary中所有值的和 例如,如果我有如下词典: NSDictionary * first = @{@"A": [NSNumber numberWithInt:1], @"B": [NSNumber numberWithInt:2], @"C": [NSNumber numberWithInt:3],

我正在寻找一种有效且简单的方法来求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]};
我想买一本字典

NSDictionary * sum = @{@"A": [NSNumber numberWithInt:2],
                         @"B": [NSNumber numberWithInt:4],
                         @"C": [NSNumber numberWithInt:6],
                         @"D": [NSNumber numberWithInt:8]};
针对不同的键进行更新

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);
NSArray*Key\u of_First=[First allkey];
NSArray*第二个键中的键=[第二个所有键];
对于(int j=0;j
针对不同的键进行更新

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);
NSArray*Key\u of_First=[First allkey];
NSArray*第二个键中的键=[第二个所有键];
对于(int j=0;j
针对不同的键进行更新

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);
NSArray*Key\u of_First=[First allkey];
NSArray*第二个键中的键=[第二个所有键];
对于(int j=0;j
针对不同的键进行更新

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);
NSArray*Key\u of_First=[First allkey];
NSArray*第二个键中的键=[第二个所有键];
对于(int j=0;j试着用这个:

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

                                                     };
试试这个:

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

                                                     };
试试这个:

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

                                                     };
试试这个:

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

                                                     };

试试这个……也许这对你有帮助

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

试试这个……也许这对你有帮助

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

试试这个……也许这对你有帮助

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

试试这个……也许这对你有帮助

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


如果您有任何问题,请告诉我。谢谢,我想到了类似的方法,但我希望我能更快地解决这个问题:D一个问题,如果某个键存在于第一个字典中而不存在于第二个字典中,会发生什么情况?无需
[第一个所有键]
;快速迭代也适用于字典。只需说:
for(NSString*key in first){…}
。如果第二个字典中不存在键,则该键的第二个字典值将为零。实际上,
b
将变为
0
,因为
[nil integerValue]
0
,这意味着结果字典将是第一个字典中的值。@MarkoZadravec,如果您有任何问题,请告诉我。这对您有帮助吗?如果您有任何问题,请告诉我。谢谢,我想到了类似的方法,但我希望我可以更快地得到它:D一个问题,如果某个键e存在于第一个字典中,但不在第二个字典中?不需要
[first allKeys]
;快速迭代也适用于字典。只需说:
for(NSString*key in first){…}
。如果第二个字典中不存在键,则该键的第二个字典值将为零。实际上,
b
将变为
0
,因为
[nil integerValue]
0
,这意味着结果字典将是第一个字典中的值。@MarkoZadravec,如果您有任何问题,请告诉我。这对您有帮助吗?如果您有任何问题,请告诉我。谢谢,我想到了类似的方法,但我希望我可以更快地得到它:D一个问题,如果某个键e存在于第一个字典中,但不在第二个字典中?不需要
[first allKeys]
;快速迭代也适用于字典。只需说:
for(NSString*key in first){…}
。如果第二个字典中不存在键,则该键的第二个字典值将为零。实际上,
b
将变为
0
,因为
[nil integerValue]
0
,这意味着结果字典将是第一个字典中的值。@MarkoZadravec,如果您有任何问题,请告诉我。这对您有帮助吗?如果您有任何问题,请告诉我。谢谢,我想到了类似的方法,但我希望我可以更快地得到它:D一个问题,如果某个键e存在于第一个字典中,但不在第二个字典中?不需要
[first allKeys]
;快速迭代也适用于字典。只需说:
for(NSString*key in first){…}
。如果第二个字典中不存在键,则该键的第二个字典值将为零。实际上,
b
将变为
0
,因为
[nil integerValue]
0
,这意味着结果字典将是第一个字典中的值。@MarkoZadravec,如果您有任何问题,请告诉我。这对您有帮助吗?如果您事先知道自己有哪些键,这种方法很好,而我没有。如果您事先知道自己有哪些键,这种方法很好,而我没有t、 如果您事先知道自己有哪些键,而我没有,那么这种方法很好。如果您事先知道自己有哪些键,而我没有,那么这种方法很好。只需将字典作为参数传递。如果键不相同,那么key的对象将返回nil,因此sum将是第一个no加零。只需将字典作为argumen传递即可t.如果键不相同,则键的对象将返回nil,因此sum将是第一个no加零。只需将字典作为参数传递。如果键不相同,则键的对象将返回nil,因此sum将是第一个no加零。只需将字典作为参数传递。如果键不相同,则键将返回nil,因此sum将是第一个no加零。