开发者

Query plist that is an array of dictionary

开发者 https://www.devze.com 2023-02-15 16:44 出处:网络
I have a plist that has an array of dicts. in the dict there is a KEY with the name UID. I want to query the plist where UID=\"1234\" .. how would I search?

I have a plist that has an array of dicts. in the dict there is a KEY with the name UID. I want to query the plist where UID="1234" .. how would I search?

sample

<array>
   &l开发者_如何学Ct;dict>
      <key>UID</key>
      <string>1234</string>
      <key>name</key>
      <string>bob</string>
   </dict>
   ....
</array>


Read in the plist as an array of dictionaries and then use filteredArrayUsingPredicate: method on NSArray:

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyInfo" ofType:@"plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:path];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"UID = %d", 1234];
NSArray *filtered = [plistData filteredArrayUsingPredicate:filter];
NSLog(@"found matches: %@", filtered);


Read in the plist as an array of dictionaries and then use the objectForKey: method of NSDictionary.

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyInfo" ofType:@"plist"];
NSArray *plistData = [[NSArray arrayWithContentsOfFile:path] retain];
for (NSDictionary *dict in plistData) {
    if ([[dict objectForKey:@"UID"] isEqualToString:@"1234"]) {
        NSLog(@"Found it!");
    }
}
0

精彩评论

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

关注公众号