Created
March 6, 2011 06:04
-
-
Save fernyb/857067 to your computer and use it in GitHub Desktop.
NSArray map method like Ruby's Array#map method
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
@interface NSArray (FRBMethods) | |
- (NSArray *)map:(id (^)(id item))block; | |
@end |
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
@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