Arcadian
Arcadian

Reputation: 4350

How do I query a plist in objective c

Is there a way to filter or query data in a plist. For example I want all objects that has Type='People'

Upvotes: 2

Views: 616

Answers (2)

Max
Max

Reputation: 16719

You can load the plist into dictionary and then get all values you want:

NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile: @"someplist.plist"];

Upvotes: 1

Kevin Sylvestre
Kevin Sylvestre

Reputation: 38032

If you load your PList into an NSArray you can use filteredArrayUsingPredicate: along with an NSPredicate to perform filtering. For example:

NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle pathForResource:@"data" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:...];
NSString *type = @"People";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Type == %@", type];
[array filteredArrayUsingPredicate:predicate];

Upvotes: 3

Related Questions