C语言中的Objective-C块 [英] Objective-C Blocks in C

查看:77
本文介绍了C语言中的Objective-C块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple Docs ,我看到了以下语句:

While reading through the blocks conceptual overview in Apple Docs, I saw the following statement:

尽管块可用于纯C和C ++,但块也可用于 始终是Objective-C对象.

Although blocks are available to pure C and C++, a block is also always an Objective-C object.

这怎么可能?我的意思是在纯C语言中可用的Objective-C对象.我很困惑.

How is this possible? I mean an Objective-C object available in pure C. I'm getting confused.

推荐答案

这怎么可能?我的意思是在纯C语言中可用的Objective-C对象.

How is this possible? I mean an Objective-C object available in pure C.

马特·加拉格尔(Matt Gallagher)撰写了一篇很好地解释了块如何工作的文章 .简而言之,将块定义为满足有效的Objective-C对象要求的结构(例如,该结构以isa指针开头).这些都不会对C造成问题,并且编译器知道块的定义是什么,因此,即使在编译普通的旧C时,它仍然可以做正确的事情来使块工作.

Matt Gallagher wrote an article that nicely explains how blocks work. In a nutshell, blocks are defined as structs that meet the requirements to be a valid Objective-C object (for example, the struct starts with an isa pointer). None of this causes a problem for C, and the compiler knows what the definition of a block is, so even when compiling plain old C it can still do the right thing to make blocks work.

添加的块的Objective-C方面不会影响块在C中的使用方式,但仍然可以将块视为对象,以便可以将它们添加到集合中,或者像其他任何对象一样进行管理.

The added Objective-C aspect of blocks doesn't affect how blocks are used in C, but still provides the ability to treat blocks as objects so that they can be added to collections and otherwise managed like any other object.

这并不奇怪.如果您考虑一下,在某种程度上,所有 Objective-C对象在C语言中都是可用的"- Foundation与Core Foundation之间的免费桥接已经很多年了.块的实现可能有所不同,但是使对象在两个环境中都能工作并不是什么新鲜事.

This isn't really so strange. If you think about it, all Objective-C objects are "available" in C at some level -- the entire Objective-C runtime consists of C functions that manipulate structures that represent Objective-C objects and classes. And even disregarding that, blocks aren't the first example of a data type that's usable in both C and Objective-C -- we've had toll free bridging between Foundation and Core Foundation for many years. The implementation of blocks may be somewhat different, but it's not a new thing to have objects that work in both worlds.

这篇关于C语言中的Objective-C块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆