Skip to content

Instantly share code, notes, and snippets.

@boredzo
Last active January 4, 2016 07:19
Show Gist options
  • Save boredzo/8587787 to your computer and use it in GitHub Desktop.
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.
extern NSArray *PRHArrayByMap(NSArray *inArray, id (^block)(id));
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