运维开发网

objective-C语言 类型而不是变量之间的插入字符,用括号括起来

运维开发网 https://www.qedev.com 2020-05-14 20:30 出处:网络 作者:运维开发网整理
我正在阅读Apple的文档,我看到了类似的东西(void(^)(void)).有人可以解释这句话的含义吗? ^是XOR,对吗?虚空XOR虚空对我来说没有多大意义? 还有类似的东西(void(^)(BOOL完成)) 这些是向Objective-C添加匿名函数和函数对象的块.参见例如 Introducing Blocks and Grand Central Dispatch : Block objec
我正在阅读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"); }];

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号