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

我正在阅读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"); }];

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...