如何将两个相同的NSDictionary合并到NSDictionary

时间:2012-05-15 03:16:30

标签: objective-c nsdictionary

这样有两个词典,两个词典具有相同的键但值不同。

/* dictionary 1 */
paramDict1 {
    payAmount = 1000;
    payType = 1;
}
/* dictionary 2 */
paramDict2 {
    payAmount = 2000;
    payType = 2;
}

I hope merge them to one dictionary like 

paramDict {
    payAmount = 1000;
    payType = 1;
    payAmount = 2000;
    payType = 2;
}

Could you tell me how to do? Thanks.

1 个答案:

答案 0 :(得分:4)

即使NSDictionary保留了键的顺序,它也没有,你要求的是不可能的,因为键必须是唯一的

相反,您可以使用字典数组:

NSArray *dicts = [NSArray arrayWithObjects: paramDict1, paramDict2, nil];

或者可能不那么惯用,唯一的键:

paramDict {
    payAmount1 = 1000;
    payType1 = 1;
    payAmount2 = 2000;
    payType2 = 2;
}