Created
March 6, 2015 13:22
-
-
Save saiday/de8ed34e88f9e096441f to your computer and use it in GitHub Desktop.
NSArray group by
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (NSDictionary *)groupByKey:(NSString *)key | |
| { | |
| NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; | |
| for (id obj in self) { | |
| id keyValue = [obj valueForKey:key]; | |
| if (keyValue) { | |
| NSMutableArray *array = dictionary[keyValue]; | |
| if (!array) { | |
| array = [NSMutableArray array]; | |
| dictionary[keyValue] = array; | |
| } | |
| [array addObject:obj]; | |
| } | |
| } | |
| return [dictionary copy]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment