Create your NSArray category and do so.
NSArray shuffle
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
- (NSArray *)shuffle | |
{ | |
NSUInteger count = [self count]; | |
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:self]; | |
for (NSUInteger i = 0; i < count; ++i) { | |
NSInteger remainingCount = count - i; | |
NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t )remainingCount); | |
[mutableArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex]; | |
} | |
return mutableArray; | |
} |
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]; | |
} |