Skip to content

Instantly share code, notes, and snippets.

@troystribling
Created April 25, 2012 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troystribling/2491439 to your computer and use it in GitHub Desktop.
Save troystribling/2491439 to your computer and use it in GitHub Desktop.
Objective C Array Map
@interface NSArray (Extensions)
- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block;
@end
@implementation NSArray (Extensions)
- (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[result addObject:block(obj, idx)];
}];
return result;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment