-
-
Save mikeabdullah/3052841 to your computer and use it in GitHub Desktop.
Objective-C Closure
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
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's handy that blocks implement
-invoke
:)