Skip to content

Instantly share code, notes, and snippets.

@quantumpotato
Created February 17, 2012 17:59
Show Gist options
  • Save quantumpotato/1854642 to your computer and use it in GitHub Desktop.
Save quantumpotato/1854642 to your computer and use it in GitHub Desktop.
For X in Y with C do {}
#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
@quantumpotato
Copy link
Author

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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment