_iVar与iVar_之间的变量命名 [英] _iVar vs. iVar_ for variable naming

查看:86
本文介绍了_iVar与iVar_之间的变量命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用前缀下划线作为实例变量命名,以区别于局部变量。

I used to use prefix underscore for instance variable naming, to distinguish from local variables.

我很高兴看到Google Objective-C风格指南,并发现它建议使用尾随下划线href =http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml?showone=Variable_Names#Variable_Names =nofollow>此处),但没有任何有关原因的详细说明。

I happend to see the "Google Objective-C Style Guide", and found that it suggests to use trailing underscores(refer it HERE), but without any detail explanation about why.

我知道这是一个编码风格问题,但我想知道使用尾随下划线有什么优点吗?

I know it is a coding style issue, but I do wonder is there any advantages of using trailing underscores?

推荐答案

相关:有关@synthesize的问题(请参阅底部的块引用答案)

优点是:_var是一个私有的约定(C99,Cocoa指南),但它是如此常见(即使在苹果模板),苹果使用双下划线__var。

Related: Question about @synthesize (see the blockquotes at the bottom of the answer)
The advantage is: _var is a convention (C99, Cocoa guidelines) for private, but it is so common (even on Apple templates) that Apple uses double underscore __var. Google solves it by using trailing underscore instead.

我用另外几个原因更新了其他答案...

I updated the other answer with a couple more reasons...

C ++中也不鼓励使用下划线(请参阅
有关使用下划线的规则是什么一个C ++标识符?)和Core Data属性
(尝试在模型中添加一个前导下划线,你将得到Name
必须以字母开头)。

Leading underscore is also discouraged in C++ (see What are the rules about using an underscore in a C++ identifier?) and Core Data properties (try adding a leading underscore in the model and you'll get "Name must begin with a letter").

尾部下划线似乎是合理的选择,但如果你喜欢
别的东西,碰撞不太可能发生,如果他们这样做,你会得到一个警告编译器。

Trailing underscore seems the sensible choice, but if you like something else, collisions are unlikely to happen, and if they do, you'll get a warning from the compiler.

这篇关于_iVar与iVar_之间的变量命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆