Created
June 6, 2012 19:30
-
-
Save setoh2000/2884115 to your computer and use it in GitHub Desktop.
BlocksKitのselectとmatchの置き換え
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
//------------------------------------------------------------------------- | |
// MSBlocks.h | |
//------------------------------------------------------------------------- | |
@interface MSBlocks : NSObject | |
+ (NSArray *)array:(NSArray *)array select:(BOOL(^)(id))block; | |
+ (id)find:(NSArray *)array match:(BOOL(^)(id))block; | |
@end | |
//------------------------------------------------------------------------- | |
// MSBlocks.m | |
//------------------------------------------------------------------------- | |
#import "MSBlocks.h" | |
@implementation MSBlocks | |
+ (NSArray *)array:(NSArray *)array select:(BOOL(^)(id))block | |
{ | |
NSMutableArray *results = [NSMutableArray arrayWithCapacity:array.count]; | |
for (NSObject *obj in array) { | |
if (block(obj)) [results addObject:obj]; | |
} | |
return results; | |
} | |
+ (id)find:(NSArray *)array match:(BOOL(^)(id))block | |
{ | |
for (NSObject *obj in array) { | |
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