Grouping of Custom Objects in Objective-C

前端 未结 6 1628
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 03:59

I have and array of custom objects of Person Class

Person : NSObject{
    NSString *firstName;
    NSString *lastName; 
    NSString *age;
}

NSMutableArray          


        
6条回答
  •  独厮守ぢ
    2021-01-12 04:37

    I have created a small library called Linq-to-ObjectiveC which provides a number of methods which make is easier to query arrays. In order to perform the group operation you require you can simply do the following:

    NSDictionary* groupedByName = [personsArray groupBy:^id(id person) {
       return [person firstName];
    }];
    

    This would return a dictionary where the keys are the distinct first names, and each value is an array of 'person' objects that have the given first name.

提交回复
热议问题