Joshua Smith
Joshua Smith

Reputation: 6641

using NSPredicate on nested dictionary/array

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.

Upvotes: 1

Views: 3128

Answers (1)

Justin Spahr-Summers
Justin Spahr-Summers

Reputation: 16973

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.

Upvotes: 4

Related Questions