如何有效地循环使用多个嵌套的NSDictionary并比较值



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

{
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 = "ntnttntttntttt";
treatHyphenAsRegular = true;
};
Text =                         {
text = "nttttHello ";
};
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 = "ntt";
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 = "ntnttntttntttt";
treatHyphenAsRegular = true;
};
Text =                         {
text = "nttttHello 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 = "ntt";
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,并提取每个属性以与另一个类似结构的NSDiction进行比较。字典可以是动态的,因为在中可能有不同xml文件的额外嵌套级别,但要比较的字典具有完全相似的结构和相同的标记。有没有一种方法可以在旅途中迭代和创建嵌套字典,然后进行并行循环,这样我就可以提取值并在2个NSDictionary之间进行比较?我已经尝试了以下代码,但我一直在寻找一种很好的方法,使它在动态创建字典的同时将值/属性与另一个字典进行比较。非常感谢您的帮助。

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个嵌套的键/值对,它们又有多个嵌套的字典。我发现在运行时很难知道哪个值有嵌套,哪个没有嵌套。

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

使用而不是"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
});
});

花点时间阅读并发编程指南。这是了解可用选项及其工作方式的好方法。

最新更新