Skip to content

Instantly share code, notes, and snippets.

@itod
Last active October 6, 2015 20:38
Show Gist options
  • Save itod/3050331 to your computer and use it in GitHub Desktop.
Save itod/3050331 to your computer and use it in GitHub Desktop.
Objective-C Closure
// compiled with ARC enabled
NSArray *foo() {
NSMutableArray *res = [NSMutableArray array];
for (int i = 0; i < 3; i++) {
[res addObject:^{
NSLog(@"%d", i);
}];
}
return res;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *funcs = foo();
for (void (^func)(void) in funcs) {
func();
}
}
}
// prints:
// 0
// 1
// 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment