加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:09:58 所属栏目:百科 来源:网络整理
导读:我正在阅读Apple的文档,我看到了类似的东西(void(^)(void)).有人可以解释这句话的含义吗? ^是XOR,对吗?虚空XOR虚空对我来说没有多大意义? 还有类似的东西(void(^)(BOOL完成)) 解决方法 这些是向Objective-C添加匿名函数和函数对象的块.参见例如 Introduci
我正在阅读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 worldn"); };

调用它:

my_block(); // prints “hello worldn”

接受块作为参数:

- (void)doSomething:(void (^)(void))block;

将该方法与内联块一起使用:

[obj doSomeThing:^(void){ printf("block was called"); }];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读