一般
NSMutableArray * mutableArray = [[NSMutableArray alloc] init];
[array enumerateObjectsUsingBlock:^(Person * person, NSUInteger idx, BOOL * _Nonnull stop) {
[mutableArray addObject:person.name];
}];
但是每次都要遍历数组感觉挺麻烦的,就搜索了一下,发现其实是有现成的API的.
一句话就搞定了.不过注意不要把key写错了。
NSArray * newArray = [array valueForKey:@"name"];
查看文档的注释:
/* Return an array containing the results of invoking -valueForKey: on each of the receiver's elements. ``The returned array will contain NSNull elements for each instance of -valueForKey: returning nil.
*/
- (id)valueForKey:(NSString *)key;