我想獲得數組只是指定在以下數組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);
http://stackoverflow.com/questions/5569632/nspredicate-to-match-any-entry-in-an-nsdatabase-with-value-that-contains-a-stri –