Arcadian
Arcadian

Reputation: 4350

Query plist that is an array of dictionary

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>
   <dict>
      <key>UID</key>
      <string>1234</string>
      <key>name</key>
      <string>bob</string>
   </dict>
   ....
</array>

Upvotes: 2

Views: 2820

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243156

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);

Upvotes: 5

Stephen Poletto
Stephen Poletto

Reputation: 3655

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!");
    }
}

Upvotes: 1

Related Questions