andyleehao
andyleehao

Reputation: 2412

How to merge two same NSDictionary to NSDictionary

There are two dictionaries like this, two dictionaries have same keys but different values.

/* 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.

Upvotes: 0

Views: 1856

Answers (1)

paulmelnikow
paulmelnikow

Reputation: 17208

Even if NSDictionary preserved the order of keys, which it does not, what you're asking for is impossible because keys must be unique.

Instead, you could use an array of dictionaries:

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

Or perhaps less idiomatically, unique keys:

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

Upvotes: 4

Related Questions