在嵌套字典/数组上使用 NSPredicate

发布于 2024-10-05 22:11:25 字数 2787 浏览 5 评论 0原文

我正在尝试使用深度嵌套的数据结构和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2024-10-12 22:11:25

由于您要过滤 [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 up images-ipad.images-ipad.categories, which doesn't exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文