Objective C /C 中的命名约定,以“_”开头?
我看到 ppl 像这样定义变量:
b2World *_world;
b2Body *_body;
CCSprite *_ball;
而不是
b2World *world;
b2Body *body;
CCSprite *ball;
我熟悉第二个,但不熟悉第一个。于是,我查阅了维基百科关于命名约定的内容:
以双下划线开头的名称 或下划线和大写字母 保留实施 (编译器、标准库)和 不应使用(例如 __reserved 或 _保留)。
那么,以“_”开头的有什么特殊含义吗?
我看到的使用“_”开头的代码在这里:
http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls
Something I see ppl define the variable like this:
b2World *_world;
b2Body *_body;
CCSprite *_ball;
instead of
b2World *world;
b2Body *body;
CCSprite *ball;
I familiar with the second one, but not the first one. So, I checked the Wikipedia about naming convention:
Names beginning with double underscore
or an underscore and a capital letter
are reserved for implementation
(compiler, standard library) and
should not be used (e.g. __reserved or
_Reserved).
So, is that any special meaning which is start with "_"?
The code I saw which using "_" to begin is here:
http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一些 Objective-C 开发人员之间有一个长期约定,即在实例变量前添加下划线。它可以在几个方面提供帮助:第一,它可以更轻松地发现 .m 文件中的实例变量;第二,它使开发人员不必为方法参数想出有创意的名称,以避免与实例变量名称发生冲突;第三,正如其他人所指出的,它表明实例变量是私有的,因此不应在整个代码中随意访问。
事实上,我主张避免在访问器(getter 和 setter)、
-dealloc
和-init...
以外的方法中直接访问实例变量。并不是说您永远不应该在其他地方使用它们,但您至少应该在其他方法中直接使用实例变量之前考虑一下。There's a long-standing convention among some Objective-C developers to prefix instance variables with an underscore. It can be helpful in several ways: one, it makes it easier to spot instance variables in a .m file; two, it relieves developers of having to come up with creative names for method parameters to avoid colliding with instance variable names; and three, as others have noted, it indicates that the instance variables are private, and therefore shouldn't be accessed willy nilly throughout the code.
In fact, I'd argue for avoiding accessing instance variables directly in methods other than accessors (getters and setters),
-dealloc
, and-init...
. Not that you should never, ever use them anywhere else, but you should at least give it some thought before using an instance variable directly in other methods.这确实很有帮助,但大多数人不知道为什么,这很遗憾。
Apple 使用下划线来分隔其他对象访问特定对象的变量的方式以及特定对象访问其自己的变量的方式。
现在这可能听起来有点奇怪,但想象一下以下情况: 您可能都认识到以下编译器警告
这将导致编译器警告,因为它不知道您是否引用了局部变量“tableView”,或者实例变量。
因此,Apple 建议您将以下内容添加到 @implementation 的顶部。
现在,当您引用
_tableView
时,编译器知道您指的是实例变量,而不是本地变量。此外,这使得理解 Obj-C 中的垃圾收集变得更加容易,并防止犯常见错误。
例如,在执行以下操作时:
现在,由于您不使用默认设置器(实际上,您不能,因为它是只读的),您需要自己保留变量。
当你给每个实例变量一个前缀时,这会更容易记住(这样你就知道你需要自己保留它)。
因此,基本上,了解
self.variable
和 (_
)variable
之间的区别非常重要。 (即:self.variable
映射到[self setVariable:...]
,而variable
直接映射到您的指针。此外,当您将其添加为私有变量,如下所示:
下划线前缀并不是真正必要的,除非您可能遇到具有相同名称的局部变量。除此之外,这也是提醒您它是本地指针的简单方法。当您将变量分配给对象时,您需要保留(并释放)该变量。
错误的是创建一个带有下划线前缀的属性,如下所示:
这确实是错误的,我错了。甚至不会解释为什么;)
所以是的!您确实应该使用下划线前缀,它使您的代码更易于阅读并由编译器解释!在 Xcode 4 中,Apple 甚至将这些 @synthesize 添加到默认模板中。
It's really really helpful, but most people don't know why, and that's a shame.
Apple uses underscores to separate the way other objects access a particular object's variables, and the way a particular object access its own variables.
Now this may sound a little bit strange, but imagine the following: You probably all recognize the following compiler warning
This will result in a compiler warning, because it does not know wether you reference to the local variable "tableView", or the instance variable.
Therefore, Apple recommends you to add the following to the top of your @implementation.
Now, when you reference to
_tableView
, the compiler knows that you mean the instance variable, and not the local one.Also, this makes it a lot easier to understand the Garbage Collection in Obj-C, and to prevent making common mistakes.
For example, when doing the following:
Now, since you do not use the default setter (actually, you can't, because it's read-only) you need to retain the variable yourself.
This is a lot easier to remember when you give every instance variable a prefix (so you know you need to retain it yourself).
So, basically, it's important to understand the difference between
self.variable
and (_
)variable
. (that is:self.variable
maps to[self setVariable:...]
andvariable
maps directly to your pointer.Furthermore, when you add it as a private variable, like this:
The underscore prefix isn't really necessary, unless you may encounter local variables that have the same name. Besides that, again, it's also an easy way to remind you that it's a local pointer and that you need to retain (and release) the variable when you assign it to your object.
What is wrong is to create a property with a underscore prefix, like this:
That's really wrong, and I'm not even gonna explain why ;)
So yes! You should really use underscore prefixes, it makes your code a lot easier to read, and to interpret by the compiler! In Xcode 4, Apple even added these
@synthesize
s to the default templates.通常,它们用于不应该在当前文件/模块/命名空间/其他内容之外访问的变量,在不支持使用
private
关键字等限制访问的语言中Usually they're used for variables that shouldn't be accessed outside the current file/module/namespace/whatever, in languages that don't support restricting access with something like a
private
keywordhttps://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html#//apple_ref/doc/uid/20001284-1001757
按照惯例和建议在上述文档中,您应该在 ivars 前面添加下划线。
诚然,它是指显式设置属性的 ivars。
但用法是相同的,无论在哪里看到 ivar,都表明它的用法。
然而,我对这种可能性持开放态度,在这种情况下,使用下划线前缀 ivar 可能会向用户发出信号,表明他们做错了什么。同时,后缀下划线可用于直接访问的纯 ivars。
这个博客有一些来自经验丰富的实践者的好想法,它建议使用带前缀的下划线。
http://blog.bignerdranch.com/463-a-motivation- for-ivar-decorations/
无论您选择使用带前缀的下划线来装饰您自己的 ivars,至少有一些证据表明某种装饰将帮助您避免错误。前缀下划线是最常见的装饰。
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html#//apple_ref/doc/uid/20001284-1001757
Both by convention and recommendation in the above document, you should prefix ivars with an underscore.
Admittedly, it is in reference to explicitly set ivars for properties.
But the usage is the same, to indicate the usage of an ivar wherever it is seen.
I am however open to the possibility, that in that context, the use of an underscore prefixed ivar could signal to the user that they are doing something wrong. Meanwhile a postfixed underscore could be used for pure ivars that are meant to be accessed directly.
This blog has some good thoughts from an experienced practitioner and it recommends using prefixed underscores.
http://blog.bignerdranch.com/463-a-motivation-for-ivar-decorations/
Wether you choose to use prefixed underscores to decorate your own ivars, there is at least some evidence that some kind of decoration will help you avoid bugs. And prefix'd underscores are the most common decoration.
Apple 为其自己的私有变量和方法保留以下划线开头的名称。在任何 Apple 平台上的 Objective-C 中,建议您不要在标识符前添加下划线。
http://developer.apple.com/ mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html
Apple reserves names beginning with underscore for its own private ivars and methods. In Objective-C on any Apple platform, it is recommended that you do not prefix your identifiers with an underscore.
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html