开发者

Unique items from a NSMutableArray with NSDictionary items?

If you have an NSMutableArray with three NSD开发者_运维问答ictionarys like this:

{ 
  name:steve, age:40;
  name:steve, age:23;
  name:paul, age:19
}

How do I turn that into an array with just two strings { steve, paul }. In other words, the unique names from the original NSMutableArray? Is there a way to do this using blocks?


Similar to the other answer, you could also do:

NSSet * names = [NSSet setWithArray:[myArray valueForKey:@"name"]];

Or

NSArray * names = [myArray valueForKeyPath:@"@distinctUnionOfObjects.name"];


something like that:

NSMutableSet* names = [[NSMutableSet alloc] init];

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)) {
  [names addObject:[obj valueForKey:@"name"]];
}];

[names allObjects] will return a NSArray of unique name

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜