Skip to content

Instantly share code, notes, and snippets.

@fernyb
Created March 6, 2011 06:04
Show Gist options
  • Save fernyb/857067 to your computer and use it in GitHub Desktop.
Save fernyb/857067 to your computer and use it in GitHub Desktop.
NSArray map method like Ruby's Array#map method
@interface NSArray (FRBMethods)
- (NSArray *)map:(id (^)(id item))block;
@end
@implementation NSArray (FRBMethods)
- (NSArray *)map:(id (^)(id item))block
{
id current;
NSMutableArray * collection = [NSMutableArray array];
NSEnumerator * e = [self objectEnumerator];
while (current = [e nextObject])
{
id result = block(current);
[collection addObject:result];
}
return (NSArray *)collection;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment