Objective-C 比较和替换 NSMutableDictionary 值的快速方法

Objective-C Quick way to compare and replace NSMutableDictionary value

我有 2 个不同长度的 NSMutableDictionary,如下所示:

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

我想把 Dic 3 做成这样:

Dic 3: {
0 = "<EPinObject: 0x17409f9a0>";
10 = "<EPinObject: 0x17409f9f0>";
...
9 = "<EPinObject: 0x17409f9f0>";}

Dic 1 具有相似的 diff 键值,所以我必须这样写

现在我正在通过使用 2 for-in 来执行此操作,但它似乎花费了太多时间和太多循环,我想知道是否有更快的方法来比较 dic1 的值与 dic2 的键并生成 dic3?

一个用于:

dict3 = [dict1 mutableCopy]
for (id<NSCopying> key in dict3.allKeys) {
    dict3[key] = dict2[dict3[key]];
}

如果你不需要覆盖dict2中不存在的值(例如,有dict1[15] == 12345,但dict2[12345] == nil),你可以添加一个nil检查:

for (id<NSCopying> key in dict3.allKeys) {
    if (dict2[dict3[key]]) {
        dict3[key] = dict2[dict3[key]];
    }
}

更新: 有了@Caleb 的改进(和一点优化):

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