Created
September 8, 2010 10:01
-
-
Save stuartcarnie/569919 to your computer and use it in GitHub Desktop.
A few Objective-C blocks additions for NSArray
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(BlocksAdditions) | |
- (NSArray*)mapUsingBlock:(id (^)(id obj))block; | |
- (NSSet*)mapToSetUsingBlock:(id (^)(id obj))block; | |
- (id)firstUsingBlock:(BOOL(^)(id obj))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
#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