在嵌套字典/数组上使用 NSPredicate
我正在尝试使用深度嵌套的数据结构和 NSPredicate 来过滤该数据。
我有一个像这样的 plist 文件:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>images-ipad</key>
<array>
<dict>
<key>categories</key>
<array>
<string>standard</string>
</array>
<key>name</key>
<string>Default</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
<dict>
<key>categories</key>
<array>
<string>standard</string>
</array>
<key>name</key>
<string>Optional</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
<dict>
<key>categories</key>
<array>
<string>christmas</string>
<string>holiday</string>
<string>standard</string>
</array>
<key>name</key>
<string>christmas</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
</array>
</dict>
</plist>
我通过 dictionWithContentsOfFile 将其加载到 NSDictionary 中。
然后我想通过 NSPredicate 获取数组 images-ipad 中“标准”类别中的元素。我认为这是可能的,但我无法实现它,并且文档似乎没有涵盖嵌套数据结构。
所以,我尝试过这个:
NSPredicate *getcat = [NSPredicate predicateWithFormat:@"ANY images-ipad.categories == 'holiday'"];
NSArray *res = [[dict objectForKey:@"images-ipad"] filteredArrayUsingPredicate:getcat];
但它不返回任何元素,这让我进入了 stackoverflow。提前致谢。
I'm trying to use a deeply nested data structure and NSPredicate to filter that data.
I have a plist file like this:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>images-ipad</key>
<array>
<dict>
<key>categories</key>
<array>
<string>standard</string>
</array>
<key>name</key>
<string>Default</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
<dict>
<key>categories</key>
<array>
<string>standard</string>
</array>
<key>name</key>
<string>Optional</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
<dict>
<key>categories</key>
<array>
<string>christmas</string>
<string>holiday</string>
<string>standard</string>
</array>
<key>name</key>
<string>christmas</string>
<key>plist</key>
<string>cardSheet.plist</string>
<key>png</key>
<string>cardSheet.png</string>
</dict>
</array>
</dict>
</plist>
Which I load into an NSDictionary via dictionWithContentsOfFile.
I then want to get the elements of the array images-ipad that are in the category 'standard' via NSPredicate. I think this is possible, but I've not been able to accomplish it, and the docs don't seem to cover nested data structures.
So, I've tried this:
NSPredicate *getcat = [NSPredicate predicateWithFormat:@"ANY images-ipad.categories == 'holiday'"];
NSArray *res = [[dict objectForKey:@"images-ipad"] filteredArrayUsingPredicate:getcat];
but it does not return any elements, which brings me to stackoverflow. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您要过滤
[dict objectForKey:@"images-ipad"]
的结果,因此您不应将该字符串包含在谓词的键路径中,因为它基本上相当于查找 < code>images-ipad.images-ipad.categories,该类别不存在。Since you're filtering the result of
[dict objectForKey:@"images-ipad"]
, you shouldn't include that string in the key path of the predicate, as it's basically equivalent to looking upimages-ipad.images-ipad.categories
, which doesn't exist.