我正在尝试获取仅在以下数组 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}
怎么获得?
请帮助或建议。
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);