如何有效地循环多个嵌套的 NSDictionaries 并比较值?

How to efficiently loop through multiple nested NSDictionaries and compare values?

我在使用 XMl reader 从此处 http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/:

解析 XMl 后得到的 NSDictionary 中有以下结构
{
Document =     {
    Page =         (
                    {
            TextLR =                 {
                Line =                     {
                    LineProps =                         {
                        applyBreakingRules = true;
                        autoDecimalTabPos = 0;
                        breakJust = BreakOptimal;
                        direction = ES;
                        hyphenationZone = 0;
                        kindAlign = Left;
                        kindJust = FullInterWord;
                        left = 0;
                        presSuppressWiggle = true;
                        rightBreak = 0;
                        rightJustify = 0;
                        text = "\n\t\n\t\t\n\t\t\t\n\t\t\t\t";
                        treatHyphenAsRegular = true;
                    };
                    Text =                         {
                        text = "\n\t\t\t\tHello ";
                    };
                    betweenBottom = false;
                    betweenTop = false;
                    bottomEnable = false;
                    break = EndPara;
                    cpLim = 12;
                    cpStart = 0;
                    direction = ES;
                    doc = Main;
                    firstLineCp = true;
                };
                bottom = 114115;
                cpLim = 12;
                cpStart = 0;
                doc = Main;
                left = 0;
                right = 2438349;
                text = "\n\t\t";
                top = 0;
            };
            cpLim = 81963072;
            fBuggyJust = false;
            fEmptyPage = false;
            fHasBubbles = false;
            fSlicedPage = false;
            height = 3448422;
            marginBottom = 3448422;
        },
                    {
            TextLR =                 {
                Line =                     {
                    LineProps =                         {
                        applyBreakingRules = true;
                        autoDecimalTabPos = 0;
                        breakJust = BreakOptimal;
                        direction = ES;
                        hyphenationZone = 0;
                        kindAlign = Left;
                        kindJust = FullInterWord;
                        left = 0;
                        presSuppressWiggle = true;
                        rightBreak = 0;
                        rightJustify = 0;
                        text = "\n\t\n\t\t\n\t\t\t\n\t\t\t\t";
                        treatHyphenAsRegular = true;
                    };
                    Text =                         {
                        text = "\n\t\t\t\tHello SO ";
                    };
                    betweenBottom = false;
                    betweenTop = false;
                    bottomEnable = false;
                    break = EndPara;
                    cpLim = 12;
                    cpStart = 0;
                    direction = ES;
                    doc = Main;
                    firstLineCp = true;
                };
                bottom = 114115;
                cpLim = 12;
                cpStart = 0;
                doc = Main;
                left = 0;
                right = 2438349;
                text = "\n\t\t";
                top = 0;
            };
            cpLim = 81963072;
            fBuggyJust = false;
            fEmptyPage = false;
            fHasBubbles = false;
            fSlicedPage = false;
            height = 3448422;
            marginBottom = 3448422;
        }
    );
    doc = "simple1.htm";
    xdpi = 72;
    xmlns = "http://apple/sites;
    "xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
    "xsi:schemaLocation" = "xmlns = "http://apple/sites/Dump.xsd";
    ydpi = 72;
};

}

我一直在努力迭代这个嵌套的 NSDictionary 并提取每个属性以与另一个结构相似的 NSDictionary 进行比较。字典可能有些动态,因为不同的 xml 文件可能有额外的嵌套级别,但要比较的字典具有完全相似的结构和相同的标签。有没有一种方法可以随时迭代和创建嵌套字典,然后进行并行循环,以便我可以提取值并与 2 个 NSDictionaries 进行比较?我试过下面的代码,但我一直在寻找一种好方法让它动态创建字典,同时将 values/attributes 与另一本字典进行比较。非常感谢帮助。

NSArray *arrPages = [[_xmlDictionary_master objectForKey:@"Document"] objectForKey:@"Page"];//this would return the array of Page dictionaries


for(int i=0;i<[arrPages count];i++){
    NSDictionary *aPage = [arrStation objectAtIndex:i];
    NSLog(@"id = %@",[aStation objectForKey:@"id"]);
}

上面的代码 returns 2 嵌套 key/value 对,它们又具有多个嵌套字典。我发现很难知道在 运行 时间内哪个值有嵌套而哪个没有。

第一个提示:重构您的数据,这样操作就不那么困难了!如果那不可行,您可以尝试一些方法。

而不是 "for loop",使用:

[arrStation enumerateObjectsWithOptions: NSEnumerationConcurrent 
                             usingBlock: ^(NSDictionary *aPage, NSUInteger idx, BOOL *stop) {
    // Code here for compare
}];

可以并行执行您的操作。

另一种技术是使用 GCD。使用 dispatch_async,然后使用 dispatch_apply 可以实现非常相似的效果。

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    dispatch_apply([arrStation count], dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t idx) {
        // Work here
    });
});

花一些时间通读 Concurrency Programming Guide。这是了解可用选项及其工作原理的好方法。