开发者

Display value from dictionary after using NSPredicate

开发者 https://www.devze.com 2023-04-03 17:55 出处:网络
I am currently using NSPredicate to search if a certain key has a certain value in plist, if it matches, I want it to display one of the values in the dictionary, not the entire dictionary, but the wa

I am currently using NSPredicate to search if a certain key has a certain value in plist, if it matches, I want it to display one of the values in the dictionary, not the entire dictionary, but the way I am using NSPredicate returns me the whole dictionary of items. May I know how to do that?

Let's say my plist looks like this:

    Root (Array)
     Item 0 (Dictionary)
          Name (String)
          Number (String)
          Address (Dictionary)
               First Address (Array)
                    Item 0 (Dictionary)
                         Block (String)            Value: 1234
                         Postal Code (String)
                         Unit Number (String)
                         Street Name (String)      Value: King Street
               Sec开发者_JS百科ond Address (Array)
                        Item 1 (Dictionary)
                         Block (String)
                         Postal Code (String)
                         Unit Number (String)
                         Street Name (String)
     Item 1 (Dictionary)
          and so on...

My NSPredicate goes like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Block == '1234'"];
NSArray *predicateArray = [blockStage filteredArrayUsingPredicate:predicate];

The NSPredicate returns me all the values in Item 0, but I want to only display the value of "Street Name", which is "King Street". How do I go about doing that???

Many thanks!!


Your fetch request will return all objects from the initial array that match your predicate. So if your initial array contains objects at the level of Item 0, this is what is returned.

If you only want to display the street name afterwards then you will need to pick this out of the returned object(s) using whatever objectForKey statements are necessary. You'll probably need some additional logic to determine which address (first or second) matches your predicate, since the keys are the same.

If your keys are really as described in the plist example above, these are not key value coding compliant. See Key-Value Coding Fundamentals for details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号