我正在阅读Apple的文档,我看到了类似的东西(void(^)(void)).有人可以解释这句话的含义吗? ^是XOR,对吗?虚空XOR虚空对我来说没有多大意义? 还有类似的东西(void(^)(BOOL完成)) 这些是向Obj
还有类似的东西(void(^)(BOOL完成))
这些是向Objective-C添加匿名函数和函数对象的块.参见例如 Introducing Blocks and Grand Central Dispatch :Block objects (informally, “blocks”) are an extension to C, as well as Objective-C and C++, that make it easy for programmers to define self-contained units of work. Blocks are similar to — but far more powerful than — traditional function pointers. The key differences are:
- Blocks can be defined inline, as “anonymous functions.”
- Blocks capture read-only copies of local variables, similar to “closures” in other languages
声明一个块变量:
void (^my_block)(void);
为其分配块对象:
my_block = ^(void){ printf("hello world\n"); };
调用它:
my_block(); // prints “hello world\n”
接受块作为参数:
- (void)doSomething:(void (^)(void))block;
将该方法与内联块一起使用:
[obj doSomeThing:^(void){ printf("block was called"); }];