Last active
October 6, 2015 20:38
-
-
Save itod/3050331 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(); | |
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