Created
August 1, 2013 13:14
-
-
Save Lewuathe/6131186 to your computer and use it in GitHub Desktop.
Blocks in Objective-c ref: http://qiita.com/Lewuathe/items/89bccc1dee2b4194e530
This file contains hidden or 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
| ^(int n) { | |
| return n + 1; | |
| }; | |
| int (^plusOne) (int) = ^(int n) { | |
| return n + 1; | |
| }; |
This file contains hidden or 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
| typedef int (^PlusOne)(int); | |
| PlusOne plusOne = ^(int n) { | |
| return n + 1; | |
| }; |
This file contains hidden or 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
| __block int x = 100; | |
| void (^print)(void) = ^(void) { | |
| NSLog("%d", x); | |
| }; | |
| print(); // 100が表示される |
This file contains hidden or 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
| - (void)oneWithFunc:(int (^)(int))func { | |
| int n = 1; | |
| return func(n); | |
| } |
This file contains hidden or 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
| [myObject oneWithFunc:^(int n) { | |
| return n * 2; | |
| }]; | |
| // 1 * 2で2が返ってくる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment