我正在阅读Apple的文档,我看到了类似的东西(void(^)(void)).有人可以解释这句话的含义吗? ^是XOR,对吗?虚空XOR虚空对我来说没有多大意义?
还有类似的东西(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"); }];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。