Created
September 24, 2009 14:39
-
-
Save dwt/192768 to your computer and use it in GitHub Desktop.
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
#import <Foundation/Foundation.h> | |
@implementation NSArray (BlocksTest) | |
- (void) do:(void (^)(id))aBlock; | |
{ | |
[self enumerateObjectsUsingBlock: | |
^(id obj, NSUInteger idx, BOOL *stop) { | |
aBlock(obj); | |
}]; | |
} | |
- (id) inject:(id)aValue into:(id (^)(id, id))aBlock; | |
{ | |
__block id sum = aValue; | |
[self do:^(id each){ | |
sum = aBlock(sum, each); | |
}]; | |
return sum; | |
} | |
@end | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
id array = [NSArray arrayWithObjects:@"first", @"second", @"third", nil]; | |
// [array do:^(id each){ | |
// NSLog(@"%@", each); | |
// }]; | |
// | |
// crashes, why? | |
NSLog(@"%@", [array inject:@"" into:(id) ^(id concatenation, id element){ | |
return [concatenation stringByAppendingString:element]; | |
// return [concatenation stringByAppendingFormat:@", %@", element]; | |
}]); | |
[pool drain]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment