术语“不透明类型”是什么意思? 在“CFBundleRef 不透明类型”的上下文中意味着什么?
有人对什么是“不透明类型”有很好的解释吗? 我在 CFBundleRef 的上下文中看到了这个术语,他们说:“CFBundleRef 不透明类型”。 这是只读类型吗?
Does someone have a good explanation of what an "opaque type" is? I saw that term in context of the CFBundleRef
, where they were saying: "CFBundleRef opaque type". Is that a type that's readonly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“不透明类型”是指您没有
struct
或class
的完整定义的类型。 在 C、C++ 和 Objective-C 中,您可以使用前向声明告诉编译器稍后将定义类型:编译器没有足够的信息让您直接使用 struct 执行任何操作> 或
class
,除了声明指向它的指针,但这通常就是您需要做的全部事情。 这允许库和框架创建者隐藏实现细节。 然后,库或框架的用户调用辅助函数来创建、操作和销毁前向声明的struct
或class
的实例。 例如,框架创建者可以为struct Foo
创建这些函数:作为 Core Foundation 框架的一部分,Apple 制作了常见的 Objective-C 类,例如
NSString
、NSArray
和NSBundle
可供 C 程序员通过不透明类型使用。 C 程序员使用指针和辅助函数来创建、操作和销毁这些 Objective-C 类的实例。 苹果称之为“免费桥接”。 它们遵循一个通用的命名约定:“CF”前缀+类名+“Ref”后缀,其中“CF”代表“Core Foundation”,“Ref”是“Reference”的缩写,意思是它是一个指针。An "opaque type" is a type where you don't have a full definition for the
struct
orclass
. In C, C++ and Objective-C, you can tell the compiler that a type will be defined later by using a forward declaration:The compiler doesn't have enough information to let you do anything directly with the
struct
orclass
except declare pointers to it, but this is frequently all you need to do. This allows library and framework creators to hide implementation details. Users of a library or framework then call helper functions to create, manipulate and destroy instances of a forward declaredstruct
orclass
. For example, a framework creator could create these functions forstruct Foo
:As part of the Core Foundation framework, Apple makes common Objective-C classes like
NSString
,NSArray
andNSBundle
available to C programmers through opaque types. C programmers use pointers and helper functions to create, manipulate and destroy instances of these Objective-C classes. Apple calls this "toll-free bridging". They follow a common naming convention: "CF" prefix + class name + "Ref" suffix, where "CF" stands for "Core Foundation" and "Ref" is short for "Reference", meaning it's a pointer.不透明类型是一种“包装”较低级别类型的类型,通常在底层实现复杂或用户不需要了解内部工作原理时使用。 苹果在这里有一个关于不透明类型的好页面:
https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/OpaqueTypes.html
例如,CFString 是一种不透明类型,因为它包装了一个字符数组,并保持其长度、其编码等,但并不直接允许用户访问这些值。 相反,它提供了访问或操作内部字段并向用户返回相关信息的方法。
An opaque type is a type that "wraps" lower-level types, and is often used when either the underlying implementation is complex, or the user simply does not need to know about the inner workings. Apple has a good page on opaque types here:
https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/OpaqueTypes.html
For example, CFString is an opaque type because it wraps a character array, maintains its length, its encoding, etc., but does not directly allow the user to access these values. Rather it provides methods that access or manipulate internal fields and return to the user the relevant information.
这是一个面向未来的结构。 例如:
如果没有“struct CFBundle”的实际定义,您的代码将无法访问 CFBundleRef 指针内的任何内容。 这是不透明的。
It's a future-declared structure. For example:
Without the actual definition of "struct CFBundle", your code cannot access anything within a CFBundleRef pointer. This is opaque.