Created
February 27, 2014 01:57
-
-
Save edom18/9242768 to your computer and use it in GitHub Desktop.
[Objective-C] Blocksがややこいのでまとめてみる ref: http://qiita.com/edo_m18/items/a1ee24cd23d3adde51d8
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
void (^blk)(void) { NSLog(@"in block"); }; |
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
(^blk)() { NSLog(@"in block"); }; |
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
typedef int (^blk_t)(int); | |
blk_t func() { | |
return ^(int i) { return i * i; }; | |
} |
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
int (^func())(int) { | |
return ^(int i) { return i * i; }; | |
} |
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
int i = 5; | |
void (^capTest)() = ^{ | |
printf("%d", i); | |
}; | |
i = 10; | |
capTest(); //=> 5になる |
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
clang -rewrite-objc main.m |
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
int func(int i) { | |
return i + 1; | |
} | |
int (*funcptr)(int) = &func; |
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
typedef void (^blk_t)(void); //`blk_t`が型となる |
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
blk_t blk = ^{ NSLog(@"in block"); }; |
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
- (void)hogeMethod:(void (^)(NSString *str))blk | |
{ | |
NSString *test = @"argument"; | |
blk(test); | |
} |
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
// [Blockの型]には`void (^)(NSString *str)`が入る | |
- (void)hogeMethod:([Blockの型])blk | |
{ | |
// | |
} |
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
void (^anyBlk)(int) = ^void (int i) { | |
printf("%d", i); | |
} | |
anyBlk(10); |
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
void (^)(int i) = … |
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
void (^anyBlk)(int i) = ^void (int i) ... |
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
struct __block_impl { | |
void *isa; | |
int Flags; | |
int Reserved; | |
void *FuncPtr; | |
}; | |
struct __main_block_impl_0 { | |
struct __block_impl impl; | |
struct __main_block_desc_0* Desc; | |
__Block_byref_i_0 *i; // by ref | |
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_i_0 *_i, int flags=0) : i(_i->__forwarding) { | |
impl.isa = &_NSConcreteStackBlock; | |
impl.Flags = flags; | |
impl.FuncPtr = fp; | |
Desc = desc; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment