Skip to content

Instantly share code, notes, and snippets.

@mikeabdullah
Forked from itod/objc_closure.m
Created July 5, 2012 10:28
Show Gist options
  • Save mikeabdullah/3052841 to your computer and use it in GitHub Desktop.
Save mikeabdullah/3052841 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();
[funcs makeObjectsPerformSelector:@selector(invoke)];
}
}
// prints:
// 0
// 1
// 2
@mikeabdullah
Copy link
Author

It's handy that blocks implement -invoke :)

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