Reputation: 3165
I'm trying to get array of just specifying key's value in following array obj.
Ex1) If specifying key "name",
wanted result is : {"James", "Jhone", "Michael", "Donald", "Mac"}
Ex2) If specifying key "age",
wanted result is : {25,27,35,25,26}
How to get?
Please help or advise.
NSArray *array = [[[NSArray alloc] initWithObjects:
@{@"name" : @"James", @"age" : @25},
@{@"name" : @"Jhone", @"age" : @27},
@{@"name" : @"Michael", @"age" : @35},
@{@"name" : @"Donald", @"age" : @25},
@{@"name" : @"Mac", @"age" : @26},
nil] autorelease];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"??? == name"];
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
NSLog(@"result : %@", filtered);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"??? == age"];
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
NSLog(@"result : %@", filtered);
Upvotes: 2
Views: 4633
Reputation: 5316
Just use KVC.
[array valueForKey:@"age"]
should give you an array of the ages.
Upvotes: 2
Reputation: 18363
Give this a try:
[array valueForKey:specifyingKey]
Hope this helps!
Upvotes: 11