问题描述
在 iPhone SDK 4.0 中,UIApplication
有一个新方法,setKeepAliveTimeout:
,它需要一个 void(^)(void)
.
In iPhone SDK 4.0, UIApplication
has a new method, setKeepAliveTimeout:
that requires a second parameter of type void(^)(void)
.
-(BOOL)setKeepAliveTimeout:(NSTimeInterval)timeout handler:(void(^)(void))keepAliveHandler
第二个参数的语法到底是什么意思,我如何声明一个可以传递给它的函数/处理程序?
What exactly does the syntax of the second parameter mean, and how would I declare a function/handler that I can pass into it?
FWIW 以下不是它想要的......
FWIW the following is not what it's looking for...
void SomeHandler( void )
{
}
推荐答案
这是一个块",是 Apple 在 Snow Leopard 中添加到 C 的新功能.更多信息请访问:
It is a "block", a new feature Apple added to C in Snow Leopard. Lots more info available at:
块对象
块对象(非正式地称为块")是 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:
块可以被内联定义为匿名函数".块捕获局部变量的只读副本,类似于其他语言中的闭包"这种功能在动态类型解释语言中很常见,但以前从未被 C 程序员广泛使用.Apple 已根据 MIT 许可将 Blocks 语言规范和我们的实现作为开源发布,添加了对 GCC 4.2 和 clang 的块支持,并已将其作为 C 编程语言下一版本的一部分提交考虑.
Blocks can be defined inline, as "anonymous functions." Blocks capture read-only copies of local variables, similar to "closures" in other languages This is kind of functionality is common in dynamically-typed interpreted languages, but has never before been widely available to C programmers. Apple has published both the Blocks Languages Specification and our implementation as open source under the MIT license, added blocks support to GCC 4.2 and clang, and has submitted it for consideration as part of the next version of the C programming language.
语法
块变量看起来像一个函数指针,除了用插入符号(‘^’)代替星号(‘*’).
A block variable looks like a function pointer, except with a caret (‘^’) instead of an asterisk (‘*’).
void (^my_block)(void);
这篇关于这个插入符号 ^ 语法,两边都有 void 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!