Objective-C:在命名变量时使用 _(下划线)
可能的重复:
在 Objective C 中使用下划线作为属性名称前缀 < /p>
在 我正在阅读的 Objective-C 书籍,在我看到的一些代码中,有时,人们会在变量名称中添加下划线。
虽然我意识到这是由于既定的约定,但我想知道:
下划线在变量名之前还是完成变量名是否有任何意义? 例如,以 _name, name 和 name_
作为 Objective-C 程序员,下划线对您来说意味着什么?
Possible Duplicate:
Prefixing property names with an underscore in Objective C
In Objective-C book i am reading and in some of the code i saw, sometimes, people add underscores to the variable names.
While i realize that this is due to an established convention, I wonder:
Is there any significance whether underscore precedes or completes variable name?
For example take _name, name and name_
As Objective-C programmer, what if anything, does underscore signify to you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这很大程度上取决于个人风格和防御性编程。但这是我个人使用并且看到人们使用该前缀的主要原因。
这是为了让您的意图更清晰,了解您是直接访问 ivar 还是使用 getter/setter。
如果我有:
和:
这将编译并生成如下所示的 getter/setter 声明:
现在要直接访问我需要使用的 ivar:
要使用 getter/setter,我可以使用点表示法或 getter/setter,如下所示:
现在我的代码,如果我不小心只是输入:
它将无法编译,因为我没有使用 getter/setter,并且没有名为
people
的 ivar,它应该是_people
。This is largely down to personal style and defensive programming. But here is the main reason why I personally use and have seen people use the prefix.
It is to do with making your intent clearer about whether you are accessing the ivar directly or using the getter/setter.
If I have:
and:
This will compile and produce the getter/setter declarations like this:
Now to directly access the ivar I need to use:
To use the getter/setter I can use dot notation or the getter/setter like:
Now in my code if I accidentally just type:
it will not compile because I am not using the getter/setter and there is no ivar called
people
it should be_people
.单个前导下划线是 Apple 内部编码约定,他们这样做是为了使他们的符号不会与你的符号冲突。不幸的是,苹果公司在发布遵循这一习惯的代码示例方面一直很草率,因此苹果公司以外的很多人都认为这是一件好事。
如果您想在 ivar 和方法名称上使用前缀,请使用除单个前导下划线之外的任何内容。
A single leading underscore is an Apple internal coding convention, and they do it so that their symbols won't collide with yours. Unfortunately, Apple's been sloppy about publishing code examples that follow this habit, so a lot of people outside of Apple have gotten the idea that it's a good thing to do.
If you want to use a prefix on your ivar and method names, use anything but a single leading underscore.
如果您确实使用此类变量名称,则绝对不应使用任何以下划线开头后跟大写字母的变量名称。 C 标准保留所有此类标识符以供任何使用(第 7.1.3 节):
即,C 语言(或给定编译器)的未来修订版可能会使用任何此类标识符作为关键字或库函数名称,这可能会破坏您的程序。有鉴于此,我宁愿根本不使用以下划线前缀的名称;你可能知道下划线后面不要使用大写字母,但这并不能阻止新人加入并这样做。
此外,所有以下划线开头的标识符都在文件范围内保留;另一个需要注意的问题。
If you do use such variable names, you definitely should not use any that begin with an underscore followed by a capital letter. The C standard reserves all such identifiers for any use (§7.1.3):
i.e., a future revision of the C language (or a given compiler) may use any such identifier as a keyword or library function name, which may break your program. In light of this, I prefer to simply not use names prefixed by an underscore at all; you may know not to use capital letters after the underscore, but that doesn't stop the new guy from coming along and doing it.
Additionally, all identifiers that start with an underscore are reserved at file scope; another gotcha to beware of.
下划线实际上并没有什么意义,但是当你使用它们时,就很难犯像 这个,但我从未见过有人在末尾使用下划线,只在开头使用,并且建议您不要在末尾添加,因为可能不清楚这些内容是什么事物 意思是。
我并没有真正在 Objective-C 代码中添加下划线,但这主要是一个品味问题,而不是意义问题。只需确保不要在名称的中间或末尾添加下划线:)
Underscore does not really mean anything, but when you do use them, it's harder to make mistakes like this one, but I have never seen anyone using underscores at the end, only at the beginning and would advise you against adding at the end as it might be unclear to what these things mean.
I don't really add underscores to my Objective-C code, but it's mostly a matter of taste and not meaning. Just make sure you don't add underscores in the middle or end of names :)
下划线表示私有,我的人员不使用它我自己的ivars,因为您始终可以使用@private,并且您可以将ivar添加到.m文件中的接口扩展,甚至代码的@implementaion部分,它已经变得常见最近随着属性的出现,您可以使用它来保护自己,避免意外访问代码中的 ivar 而不是属性,因为编译器可以告诉您错误,在 C 编程中,我经常使用前导下划线来表明函数不是自包含,它可以被另一个函数使用,并且也会在 C++ 中从 ivar 使用它,因为 C++ 不喜欢成员函数和 ivars 有相同的名称。
underscore mean private, I personnel don't use it my self ivars, since you can always use @private and you can add ivar to the interface extension in the .m file ore even the @implementaion section of your code, it has become common recently with the advent of properties so you can use it to protected yourself form accidentally accessing an ivar in code instead of the property since the compiler can the tell you of the error, in c programming i will ofter use the leading under score to show that a function is not self contained, it to be used by another function and will also use it in C++ from ivar because C++ doesn't like a member functions and ivars to have the same name.