使用下划线作为前缀命名实例变量在 Cocoa (Objective-C) 中是否有任何副作用?

发布于 2024-10-31 16:46:49 字数 639 浏览 1 评论 0原文

可能的重复:
可可 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写下不归期 2024-11-07 16:46:49

没有副作用。但是,在访问私有 ivars 和函数参数名称时,使用下划线可以更轻松地阅读代码。

例如,假设您的头文件中有 NSString *_name;
然后在您的代码中将如下所示:

- (void)doSomethingWithName:(NSString *)name {
    // the underscore makes reading code easier
    _name = [name retain];
    name = [_name retain];  // you know right away that this is wrong
}

我个人遵循 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:

- (void)doSomethingWithName:(NSString *)name {
    // the underscore makes reading code easier
    _name = [name retain];
    name = [_name retain];  // you know right away that this is wrong
}

I personally follow Google Objective-C Style Guide and use a trailing underscore. So my ivars would be: NSString *name_;

香草可樂 2024-11-07 16:46:49

老实说,我在一个非常大的项目中用下划线命名了所有本地私有变量,事情开始看起来像这样。
_thisVar
_thatVar
_thisSavesSomething

在我 10 年的编码和从事大大小小的项目中(我做过这个,没有做过这个)。
不要这样做。这完全是浪费时间。 (抱歉大喊大叫)。

我的理由是。

  1. 它丑化了代码,我讨厌丑陋的代码。
  2. 它会为您使用和引用的每个变量添加不必要的输入。
  3. 它使自动完成变得更加无用,所以现在你必须输入“_”+变量名字,然后 xcode 自动完成才能更好地匹配你的变量。
  4. 您已经可以在头文件中显式地将变量设为私有和公共,因此无需在所有变量的开头添加新的命名约定,例如“_”。

我可能会使用“_”来表示真正私密的东西,我想表明这是特殊的。但我几乎永远不会使用它。

希望这能解决问题。约翰.

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.

  1. It uglifies the code, I hate ugly code.
  2. It ads unnecessary typing for each variable you use and reference.
  3. It makes autocomplete a little bit more useless so now you have to type "_" + variable first name before xcode autocomplete can make a better match for your variable.
  4. You already can explicitly make variables private and public in your header file so no need to add a new naming convention eg "_" at the start of all your variables.

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.

烟酒忠诚 2024-11-07 16:46:49

与约翰相反,我总是在局部变量前加上下划线,一个主要原因是——它可以防止您意外使用私有变量而不是公共变量。唯一一次应该访问私有变量是在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文