objective-c – C和Objective C中块之间的差异

前端之家收集整理的这篇文章主要介绍了objective-c – C和Objective C中块之间的差异前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是 Blocks Programming Guide Conceptual Overview部分的摘录

You can copy a block and even pass it to other threads for deferred
execution (or,within its own thread,to a runloop). The compiler and
runtime arrange that all variables referenced from the block are
preserved for the life of all copies of the block. Although blocks are
available to pure C and C++,a block is also always an Objective-C
object.

我一直试图从最后一句话中弄清楚,但没有成功.句子的第一部分和第二部分似乎与我不相容(我可能遗漏了一些东西).这是否意味着块在C/C++和目标C中不是一回事?这是由于捕获了目标C中的块对象的方式吗?

解决方法

好吧,经过一些环顾四周,我找到了一些指向答案的指针(没有双关语意).铿锵声 block language specification说明了Objective-C Extensions

Objective-C extends the definition of a Block reference type to be
that also of id. A variable or expression of Block type may be
messaged or used as a parameter wherever an id may be. The converse is
also true. Block references may thus appear as properties and are
subject to the assign,retain,and copy attribute logic that is
reserved for objects.

All Blocks are constructed to be Objective-C objects regardless of
whether the Objective-C runtime is operational in the program or not.
Blocks using automatic (stack) memory are objects and may be messaged,
although they may not be assigned into __weak locations if garbage
collection is enabled.

尽管我仍然对块运行时是否以相同的方式处理Objective C和C(即使编译纯C也创建Objective C对象)以及apple’s proposed extension to C是否允许在C/C++中使用块而不创建Objective C对象.欢迎评论.

原文链接:https://www.f2er.com/c/119017.html

猜你在找的C&C++相关文章