在Objective C中用下划线前缀属性名称 [英] Prefixing property names with an underscore in Objective C

查看:28
本文介绍了在Objective C中用下划线前缀属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前避免在变量名中使用下划线,这可能是我大学 Java 时代的遗留问题.所以当我在 Objective C 中定义一个属性时,这就是我自然要做的.

I've previously avoided underscores in my variable names, perhaps a holdover from my college Java days. So when I define a property in Objective C this is what I naturally do.

// In the header
@interface Whatever
{
    NSString *myStringProperty
}

@property (nonatomic, copy) NSString *myStringProperty;

// In the implementation
@synthesize myStringProperty;

但几乎在每个例子中都是这样完成的

But in almost every example it is done like

// In the header
@interface Whatever
{
    NSString *_myStringProperty
}

@property (nonatomic, copy) NSString *myStringProperty;

// In the implementation
@synthesize myStringProperty = _myStringProperty;

我是否应该克服对下划线的厌恶,因为这是应该做的一种方式,这种样式是否有充分的理由成为首选?

Should I get over my aversion to the underscore because that is the one way it should be done, is there a good reason for this style being the preferred one?

更新:现在有了自动属性合成,您可以省略@synthesize,结果与您使用的结果相同

Update: With automatic property synthesis nowadays you can leave out the @synthesize and the result is the same as if you'd used

@synthesize myStringProperty = _myStringProperty;

这清楚地向您展示了 Apple 的偏好.从那以后,我学会了不再担心并喜欢下划线.

which clearly shows you Apple's preference. I've since learned to stop worrying and love the underscore.

推荐答案

我总是使用下划线.它明确区分了局部变量和实例变量.它还可以避免以下情况下的编译器警告:

I always use underscores. It creates a clear distinction between local variables and instance variables. It also avoids compiler warnings in the following situation:

@interface MyClass
{
    NSString *name
}

@property (nonatomic, copy) NSString *name;

- (id) initWithName:(NSString *) name;
@end

@implementation MyClass

@synthesize name;

// The following method will result in a compiler warning
// (parameter name same as ivar name)
- (id) initWithName:(NSString *) name {
   if (self = [super init]) {
      self.name = name;
   }

   return self;
}

@end

编辑:

在不得不忍受反对票并通读评论后,让我尝试说明我的观点:

After having to endure downvotes and reading through the comments, let me try to make my point:

Apple 建议 ivars 与其属性具有相同的名称.Apple 还建议属性以小写字母开头.而且 Apple 还建议局部变量以小写字母开头.

Apple recommends that ivars have the same name as their property. Apple also recommends that properties start with a lowercase letter. And Apple also recommends that local variables start with a lowercase letter.

现在你有一个问题,因为当你阅读一段代码时,你看到一个正在使用的变量,你不能通过命名约定来判断这个变量是一个 ivar 还是一个局部变量.那太糟糕了.解决方案是对 ivars 和局部变量使用不同的命名约定.这只是常识.

Now you have a problem, because when you read a piece of code, and you see a variable being used, you cant' tell by the naming convention if this variable is an ivar or a local variable. That sucks. The solution is to have different naming conventions for ivars and local variables. That's just plain common sense.

您实现此命名约定的方式无关紧要.如果你真的想要,你可以简单地将_WOOHAHA"附加到 ivar 名称.我不在乎(但也许其他人会).问题是知道自己在做什么的人决定为 ivars 使用下划线前缀".恕我直言,他们做出了正确的决定,即使他们自己的公司推荐其他东西.(我所说的开发人员是编写一些主要的 Apple 框架和 .NET Framework 类的人)

The way you implement this naming convention is irrelevant. If you really want, you can simply append "_WOOHAHA" to the ivar names. I don't care (but maybe others will). The thing is that people who know what they're doing have decided to go with the "underscore prefix" for ivars. IMHO, they made the right decision, even if their own company recommends something else. (the developers I'm talking about are the people writing some major Apple frameworks and the .NET Framework classes)

归根结底,代码质量比遵循一个甚至不被鼓吹的人遵守的愚蠢规则更重要.

关于您展示的代码的另一条评论:永远不要在字符串属性上使用 retain.您应该改用复制.

Another remark about the code you've shown: never use retain on string properties. You should use copy instead.

有关复制/保留属性的更多信息,请参阅:

For more info about copy/retain on properties, see:

NSString 属性:复制还是保留?

这篇关于在Objective C中用下划线前缀属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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