Reputation: 9923
I have 2 different length NSMutableDictionary like this:
Dic 1: {
0 = 22962;
10 = 11762;
11 = 11762;
12 = 11761;
13 = 11761;
2 = 11762;
3 = 11761;
4 = 11763;
5 = 11763;
6 = 11763;
7 = 11763;
8 = 11763;
9 = 11762;}
Dic 2: {
11756 = "<EPinObject: 0x17009f860>";
11761 = "<EPinObject: 0x17409f950>";
11757 = "<EPinObject: 0x1700973e0>";
11762 = "<EPinObject: 0x17409f9f0>";
11758 = "<EPinObject: 0x174280410>";
11763 = "<EPinObject: 0x17409fa40>";
11759 = "<EPinObject: 0x174280460>";
11760 = "<EPinObject: 0x1742804b0>";
22962 = "<EPinObject: 0x17409f9a0>";}
I want to make Dic 3 like this:
Dic 3: {
0 = "<EPinObject: 0x17409f9a0>";
10 = "<EPinObject: 0x17409f9f0>";
...
9 = "<EPinObject: 0x17409f9f0>";}
Dic 1 have similar value for diff key so i have to put like that
Right now im doing this by using 2 for-in but it seems takes too long and too many loops, i wonder if theres faster way to compare the value of dic1 with key of dic2 and generate the dic3?
Upvotes: 1
Views: 69
Reputation: 7764
One for:
dict3 = [dict1 mutableCopy]
for (id<NSCopying> key in dict3.allKeys) {
dict3[key] = dict2[dict3[key]];
}
If you don't need to overwrite values that are not present in the dict2
(for instance, there is dict1[15] == 12345
, but dict2[12345] == nil
), you can add a nil check:
for (id<NSCopying> key in dict3.allKeys) {
if (dict2[dict3[key]]) {
dict3[key] = dict2[dict3[key]];
}
}
UPDATE: With @Caleb's improvements (and a little optimization):
for (id key in dict1) {
id dict3Value = dict3[key]; //so we don't have to get this value twice
if (dict2[dict3Value]) {
dict3[key] = dict2[dict3Value];
}
}
Upvotes: 4