Getting an NSArray of a single attribute from an NSArray

后端 未结 3 803
一整个雨季
一整个雨季 2021-01-02 09:08

I am facing a very regular scenario.

I have an NSArray which has object of a custom type, say Person. The Person class has the attributes: firstName, lastName and ag

3条回答
  •  清酒与你
    2021-01-02 09:45

    You can also use block based enumeration:

    NSArray *people;  // assumably has a bunch of people
    NSMutableArray *firstNames = [NSMutableArray array];
    
    [people enumerateObjectsUsingBlock: 
     ^(id obj, NSUInteger idx, BOOL*flag){
         // filter however you want...
         [firstNames addObject:[Person firstName]];
     }];
    

    The benefit is it is fast and efficient if you have a bunch of people...

提交回复
热议问题