Objective-C 中带下划线前缀的变量名是什么意思?

发布于 2024-09-26 02:32:52 字数 135 浏览 7 评论 0原文

我注意到,在许多社区 Objective-C 类和 Apple 的框架中,他们使用以下划线作为变量前缀的约定来命名某些变量,例如:_name。有下划线的原因是什么。我应该在自己的课堂上这样做吗?如果是这样,我应该在何时何地使用它?

I noticed that in many community Objective-C classes and in Apple's frameworks they name some of the variables using a convention that prefixes variables with an underscore, such as: _name. What is the reason for having the underscore. Should I be doing this in my own classes? If so where and when should I use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小耗子 2024-10-03 02:32:52

这就是所谓的丑化。关键是您从不使用它,因此您创建的任何变量名或#define都不会干扰Apple的代码。

讽刺的是,许多人使用这样的名称创建标头防护,因为他们看到系统标头这样做。

来自 C99 7.1.3“保留标识符”:

所有以下划线开头的标识符以及大写字母或其他下划线始终保留供任何使用。

(它们的意思是为系统库保留。)

注意:我不确定 C99 和 Apple ObjC 之间的确切关系,但您可能还拥有适用于整个 C 语言系列的命名约定。特别是 ObjC++ 需要有效的 C++ 名称,其中还要求任何地方都不能有双下划线。

That's called uglification. The point is that you never use it, so no variable name or #define you create could ever interfere with Apple's code.

Ironically, many people create header guards with such names, because they see the system headers do it.

From C99 7.1.3 "Reserved identifiers":

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

(They mean reserved for the system library.)

Note: I'm not sure of the exact relationship between C99 and Apple ObjC, but you might as well have naming conventions that work across the entire C language family. In particular ObjC++ would require valid C++ names, which have the additional requirement of no double underscores anywhere.

债姬 2024-10-03 02:32:52

在 Cocoa 中,这是一个约定,表明某些东西是私有的,不应该在外部使用。然而,这是非官方的约定,特别是考虑到 文档

以“_”(单个下划线字符)开头的方法名称保留供 Apple 使用。

然而,该建议特别适用于方法,而不是变量。因此,如果您想在变量前添加下划线前缀,请直接进行。话虽这么说,如果您使用下划线前缀来指示某些数据的私有性质,也许您不应该首先公开它......

In Cocoa, it's a convention to indicate the something is private and shouldn't be used externally. However, it's unofficial convention, particularly in light of wording like this in the documentation:

Method names beginning with “_”, a single underscore character, are reserved for use by Apple.

However, that recommendation specifically applies to methods, not variables. So if you'd like to prefix your variables with underscores, go right ahead. That being said, if you're using the underscore prefix to indicate the private nature of some data, perhaps you shouldn't be exposing it in the first place...

旧情勿念 2024-10-03 02:32:52

下划线会妨碍可读性。另外,使用 LLVM 代替 GCC,我摆脱了标头侧 ivars 并使用标头侧属性。确保使您的属性成为非原子属性,除非您确实希望同步读取和写入以保证线程安全。除非您指定非原子性,否则它将默认为原子性 - 这将剥夺您的一些性能。

同样按照惯例,永远不要以 get 开头访问器。 setters 应该以 set 开头,但 getters 不能以 get 开头。阅读 KVO 和 KVC,了解有关约定及其用途的更多信息。

不过,我确实喜欢枚举命名列表中的下划线。在这里,下划线帮助我找出 5 行或更多行中所有以相同词干开头的后缀。
喜欢
typedef NSInteger COMPASS_DIRECTION;
枚举{
指南针方向北,
COMPASS_DIRECTION_EAST,
指南针方向南,
COMPASS_DIRECTION_WEST,
};

Underscores get in the way of readability. Also with LLVM in place of GCC, I am getting rid of header side ivars and using header side properties. Be sure to make your properties non atomic unless you really want reads and writes synchronized for thread safety. unless you specify non atomic, it will default to atomic - which will deprive you of some performance.

also by convention, never start accessors with get. setters Should start with set but no getters with get. Read up on KVO and KVC for more about the conventions and what they are good for.

I do however like underscores in Enumeration naming list. Here the underscores help me pick out the suffix in 5 or more lines that all start with the same stem.
Like
typedef NSInteger COMPASS_DIRECTION;
enum {
COMPASS_DIRECTION_NORTH,
COMPASS_DIRECTION_EAST,
COMPASS_DIRECTION_SOUTH,
COMPASS_DIRECTION_WEST,
};

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