为什么Cocoa-Touch类的ivars具有前导下划线字符? [英] Why do Cocoa-Touch class ivars have leading underscore character?
问题描述
这个约定有什么目的吗?
Is there some purpose for this convention?
推荐答案
Apple喜欢使用下划线来表示 Cocoa的编码指南:
Apple likes to use underscores to mean "private", according to the Coding Guidelines for Cocoa:
避免使用下划线字符作为前缀,方法。苹果保留使用这个约定。第三方使用可能导致命名空间冲突;他们可能会不小心使用自己的方法覆盖现有的私有方法,造成灾难性的后果。
Avoid the use of the underscore character as a prefix meaning private, especially in methods. Apple reserves the use of this convention. Use by third parties could result in name-space collisions; they might unwittingly override an existing private method with one of their own, with disastrous consequences.
以下划线开头的方法名称 Objective-C编程语言(这意味着即使你不使用Cocoa也可以保留它们):
Method names beginning with underscores are reserved according to The Objective-C Programming Language (which means they're reserved even if you don't use Cocoa, presumably):
以_开头的方法名称,单个下划线字符保留供Apple使用。
Method names beginning with "_", a single underscore character, are reserved for use by Apple.
C / C ++约定是(通常)保留用于实现的前导下划线。很多人误解了这一点,并使用_任何私人;导致_FooLog()调用在我们的代码库的一个大块中的扩散,即使它调用未定义的行为。
Additionally, the C/C++ convention is that leading underscores are (often) reserved for the implementation. A lot of people misinterpret this and use _ for anything "private"; leading to the proliferation of _FooLog() calls in a large chunk of our codebase, even though it invokes undefined behaviour.
这么做的唯一原因是阻止直接ivar访问在您自己的类。使用 @private
阻止其他类的ivar访问。
The only reason to do it is to discourage direct ivar access in your own class. Prevent ivar access from other classes with @private
.
这篇关于为什么Cocoa-Touch类的ivars具有前导下划线字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!