类型之间的插入字符而不是变量,用括号括起来 [英] Caret character between types rather than variables, surrounded by parentheses
问题描述
我正在浏览 Apple 的文档,我看到了类似这样的 (void (^)(void))
.有人能解释一下这个说法是什么意思吗?^
是异或,对吧?void XOR void
对我来说没有多大意义?
I was going through Apple's documentation and I saw something like this (void (^)(void))
. Can somebody explain what this statement means? ^
is XOR, right? void XOR void
doesn't makes much sense to me?
还有类似(void (^)(BOOL finished))
推荐答案
这些是向 Objective-C 添加匿名函数和函数对象的块.见例如介绍 Blocks 和 Grand中央调度:
These are blocks which add anonymous functions and function objects to Objective-C. See e.g. Introducing Blocks and Grand Central Dispatch :
块对象(非正式地称为块")是 C 以及 Objective-C 和 C++ 的扩展,它使程序员可以轻松定义自包含的工作单元.块类似于 - 但比 - 传统的函数指针强大得多.主要区别在于:
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:
- 块可以被内联定义为匿名函数".
- 块捕获局部变量的只读副本,类似于其他语言中的闭包"
声明一个块变量:
void (^my_block)(void);
为其分配一个块对象:
my_block = ^(void){ printf("hello world
"); };
调用它:
my_block(); // prints "hello world
"
接受一个块作为参数:
- (void)doSomething:(void (^)(void))block;
将该方法与内联块一起使用:
Using that method with an inline block:
[obj doSomeThing:^(void){ printf("block was called"); }];
这篇关于类型之间的插入字符而不是变量,用括号括起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!