2

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>
Arcadian
  • 4,312
  • 12
  • 64
  • 107
  • possible duplicate of [Array from Array with dictionaries](http://stackoverflow.com/questions/2680403/array-from-array-with-dictionaries) – David Gelhar Mar 09 '11 at 18:12
  • @DavidGelhar: Perhaps not; it seems like @magic-c0d3r wants to *filter* the array, not extract values from each object. – Dave DeLong Mar 09 '11 at 18:46

2 Answers2

5

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);
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1

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!");
    }
}
Stephen Poletto
  • 3,645
  • 24
  • 24