0

我有一个NSMutableDictionary从 JSON 字符串创建的。

问题是我需要在 aNSArray和 a之间进行一些非常复杂的数据比较NSMutableDictionary。我想不出一个有效的方法来做到这一点。

字典看起来像这样:

dictionaryMain

0 = {
    key1 = value1;
    key2 = value2;
    key3 = {
        subKey1 = {
            subSubKey1 = {
                inner1 = apples;
                inner2 = oranges;
            };
            subSubKey2 = {
                inner1 = grapes;
                inner2 = lemons;
            };
            subSubKey3 = {
                inner1 = mangoes;
                inner2 = kiwis;
            };
        };
    };
    key4 = {
        subKey1 = {
            subSubKey1 = {
                inner1 = pineapples;
                inner2 = apples;
            };
            subSubKey2 = {
                inner1 = oranges;
                inner2 = mangoes;
            };
        };
    };
};

1 = {
    key1 = value1;
    key2 = value2;
    key3 = {
        subKey1 = {
            subSubKey1 = {
                inner1 = watermelon;
                inner2 = grapes;
            };
            subSubKey7 = {
                inner1 = bananas;
                inner2 = oranges;
            };
        };
    };
    key4 = {
        subKey1 = {
            subSubKey5 = {
                inner1 = kiwis;
                inner2 = mangoes;
            };
        };
    };
};

// ... and so on

我有一个数组arrayOfKeys,其中有一些subSubKeys 如下:

arrayOfKeys

{
    subSubKey5,
    subSubKey7,
    subSubKey10
}

我需要遍历 myNSMutableDictionary并仅选择那些具有与arrayOfKeys. 这些选定的对象将被添加到新字典中。

例如,如果arrayOfKeyscontains subSubKey7,则对象 [1]将从中选择,NSMutableDictionary因为它内部深处的键具有 name subSubKey7

我试图找出一种简单的方法来做到这一点,但我想不出一个可行的解决方案。我唯一能想到的就是使用大量的嵌套循环和肮脏的快速代码。

我花了两天时间试图弄清楚这一点。有什么办法解决这个问题,各位?

谢谢!

4

1 回答 1

2

您应该使用递归方法(或函数)来检查它传递的字典并调用自身来处理任何子字典。该方法应该返回它找到的任何东西以及它调用自身时找到的任何东西。

如果应该只有一个结果数组(从一个级别),则该方法可以在找到后立即返回,或者在调用自身的结果为非零时返回。

于 2013-07-29T21:57:28.773 回答