使用下划线作为前缀命名实例变量在 Cocoa (Objective-C) 中是否有任何副作用?
我发现在 Apple 框架的头文件中,Apple 名称实例变量在类接口内带有前缀 underscope。 就像下面的 _delegate 实例一样:
@interface ClassName : NSObject {
id _delegate;
}
@end
但是,如果我们在定义自己的实例变量时遵循这个命名约定,会有副作用吗?我已经寻找这个问题的答案很长时间了。
在苹果的代码指南中,苹果只是说他们保留以下划线开头的方法名称,他们没有提到任何关于实例变量命名问题的限制。
我的同事说,如果您定义以下划线开头的实例变量,如果您选择的名称存在于框架的私有头文件中,则可能会与框架发生冲突。这是否可能,或者这是否成为我们不应该使用以下划线开头的名称的原因,因为苹果可能已经使用了它?
Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?
I found that in Apple's frameworks' header files , Apple name instance variable with prefix underscope inside a class interface.
like the _delegate instance below:
@interface ClassName : NSObject {
id _delegate;
}
@end
But is there any side effects if we follow this naming convention when defining our own instance variable? I've been searching the answer for this question for quite a long time.
In apple's Code Guideline, apple just said they reserve the methods name begin with underscore, they haven't mention any restriction about instance variable's naming problem.
My colleague said if you define instance variable begin with underscore might get collide with the framework if the name you pick exist in the framework's private header file. Is this possible or does this become a reason that we shouldn't use the name begin with underscore because apple might already used it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有副作用。但是,在访问私有 ivars 和函数参数名称时,使用下划线可以更轻松地阅读代码。
例如,假设您的头文件中有
NSString *_name;
。然后在您的代码中将如下所示:
我个人遵循 Google Objective-C 风格指南< /a> 并使用尾随下划线。所以我的 ivars 是:
NSString *name_;
There is no side-effect. But using an underscore makes reading code a lot easier when accessing private ivars and function parameter names.
For example let say you have
NSString *_name;
in your header file.Then in your code would read like so:
I personally follow Google Objective-C Style Guide and use a trailing underscore. So my ivars would be:
NSString *name_;
老实说,我在一个非常大的项目中用下划线命名了所有本地私有变量,事情开始看起来像这样。
_thisVar
_thatVar
_thisSavesSomething
在我 10 年的编码和从事大大小小的项目中(我做过这个,没有做过这个)。
不要这样做。这完全是浪费时间。 (抱歉大喊大叫)。
我的理由是。
我可能会使用“_”来表示真正私密的东西,我想表明这是特殊的。但我几乎永远不会使用它。
希望这能解决问题。约翰.
To be honest, I named all my local private variable all with an underscore in a really big project and things start looking like this.
_thisVar
_thatVar
_thisSavesSomething
In my 10 years of coding and working on big and small projects (where I have done this and not done this).
DONT DO IT. It is a total WASTE OF TIME. (sorry for yelling).
My reasons are.
I might use an "_" for something really private, that I wanted to indicate that this is special. But I will pretty much never use it.
Hope this clears things up. John.
与约翰相反,我总是在局部变量前加上下划线,一个主要原因是——它可以防止您意外使用私有变量而不是公共变量。唯一一次应该访问私有变量是在 init、dealloc 和评估器方法中。意外使用私有变量可能会导致难以追踪的错误和内存泄漏。
In counter to John, I always prefix my local variables with an underscore, for one major reason - it prevents you from accidentally using the private variable instead of a public variable. The only time a private variable should be accessed is in init, dealloc, and assessor methods. Using a private variable accidentally can lead to bugs that can be difficult to track down and memory leaks.