Last active
January 4, 2016 07:19
-
-
Save boredzo/8587787 to your computer and use it in GitHub Desktop.
Function to create an NSArray with the results of calling a block on every object from an existing NSArray.
This file contains 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
extern NSArray *PRHArrayByMap(NSArray *inArray, id (^block)(id)); |
This file contains 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 *PRHArrayByMap(NSArray *inArray, id (^block)(id)) { | |
if (inArray == nil) | |
return nil; | |
NSCParameterAssert(block != nil); | |
NSMutableArray *outArray = [NSMutableArray arrayWithCapacity:inArray.count]; | |
for (id obj in inArray) { | |
id result = block(obj); | |
NSCAssert(result != nil, @"%s: block(%@) returned nil—array follows\n%@", __func__, obj, inArray); | |
[outArray addObject:result]; | |
} | |
return outArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment