Skip to content

Instantly share code, notes, and snippets.

@stuartcarnie
Created September 8, 2010 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartcarnie/569919 to your computer and use it in GitHub Desktop.
Save stuartcarnie/569919 to your computer and use it in GitHub Desktop.
A few Objective-C blocks additions for NSArray
@interface NSArray(BlocksAdditions)
- (NSArray*)mapUsingBlock:(id (^)(id obj))block;
- (NSSet*)mapToSetUsingBlock:(id (^)(id obj))block;
- (id)firstUsingBlock:(BOOL(^)(id obj))block;
@end
#import "BlocksAdditions.h"
#pragma mark -
@implementation NSArray(BlocksAdditions)
- (NSArray*)mapUsingBlock:(id (^)(id obj))block {
NSMutableArray *new = [NSMutableArray array];
for(id obj in self)
{
id newObj = block(obj);
[new addObject: newObj ? newObj : [NSNull null]];
}
return new;
}
- (NSSet*)mapToSetUsingBlock:(id (^)(id obj))block {
NSMutableSet *new = [NSMutableSet set];
for (id obj in self) {
id newObj = block(obj);
[new addObject:newObj ? newObj : [NSNull null]];
}
return new;
}
- (id)firstUsingBlock:(BOOL(^)(id obj))block {
for(id obj in self) {
if (block(obj))
return obj;
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment