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

时间:2015-04-29 23:53:07

标签: objective-c xml nsdictionary

我在NSDictionary中有以下结构,我在使用XMl阅读器从http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/解析XMl后得到了:

{
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之间进行比较?我已经尝试了下面的代码,但是我一直在寻找一种好的方法来让它动态地创建字典,同时将值/属性与另一个字典进行比较。非常感谢帮助。

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

上面的代码返回2个嵌套的键/值对,后者又有多个嵌套的词典。我发现很难知道哪个值有嵌套,哪个值在运行时没有。

1 个答案:

答案 0 :(得分:0)

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

使用:

而不是“for循环”
has_many :books, dependent: :destroy

可以并行执行您的操作。

另一种技术是使用GCD。使用dispatch_async,dispatch_apply可以实现非常相似的功能。

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

花一些时间阅读Concurrency Programming Guide。这是了解哪些选项及其工作方式的好方法。