Created
February 17, 2012 17:59
-
-
Save quantumpotato/1854642 to your computer and use it in GitHub Desktop.
For X in Y with C do {}
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 "BlockHacking.h" | |
typedef void (^twoObjectBlock)(id a, id b); | |
typedef void (^XYZBlock)(Class x, NSArray *y, id c, twoObjectBlock b); | |
XYZBlock arrayDoBlock = ^(Class x, NSArray *y, id c, twoObjectBlock b) { | |
for (x in y) { | |
b(c,x); | |
} | |
}; | |
@implementation BlockHacking | |
+ (void)hackBlocks { | |
NSMutableArray *mutableArray = [NSMutableArray array]; | |
NSArray *fooArray = [NSArray arrayWithObjects:@"alpha", @"beta", @"gamma", @"delta", @"epsilon", nil]; | |
twoObjectBlock logAndStoreBlock = ^(id a, id b) { | |
NSLog(b); | |
[(NSMutableArray *)a addObject:b]; | |
}; | |
arrayDoBlock([NSString class], fooArray, mutableArray, logAndStoreBlock); | |
NSLog(@"mutable array is: %@",mutableArray); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
2012-02-17 12:06:29.003 HackTest[1468:207] alpha 2012-02-17 12:06:29.004 HackTest[1468:207] beta 2012-02-17 12:06:29.005 HackTest[1468:207] gamma 2012-02-17 12:06:29.006 HackTest[1468:207] delta 2012-02-17 12:06:29.007 HackTest[1468:207] epsilon 2012-02-17 12:06:29.007 HackTest[1468:207] mutable array is: ( alpha, beta, gamma, delta, epsilon )