在Objective C中使用下划线来代替属性名称 [英] Prefixing property names with an underscore in Objective C

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

问题描述

我之前避免了我的变量名称中的下划线,也许是我的大学Java时代的延期。因此,当我在Objective C中定义一个属性时,这是我自然而然的做法。

  //在标题中
@接口Whatever
{
NSString * myStringProperty
}

@property(nonatomic,copy)NSString * myStringProperty;

//在实现中
@synthesize myStringProperty;

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

  //在标题中
@interface无论
{
NSString * _myStringProperty
}

@属性(nonatomic,copy)NSString * myStringProperty;

//在实现
@synthesize myStringProperty = _myStringProperty;

我应该覆盖我对下划线的厌恶,因为这是一个应该做的方法,

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

相同

  @synthesize myStringProperty = _myStringProperty; 

这清楚地显示了苹果的偏好。

解决方案

我总是使用下划线。它创建了局部变量和实例变量之间的明显区别。它还避免在以下情况下的编译器警告:

  @interface MyClass 
{
NSString * name
}

@property(nonatomic,copy)NSString * name;

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

@implementation MyClass

@synthesize name;

//以下方法将导致编译器警告
//(参数名称与ivar名称相同)
- (id)initWithName:(NSString *)name {
if(self = [super init]){
self.name = name;
}

return self;
}

@end

strong>:



在忍受downvote和阅读评论之后,让我尝试说明一下:



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



现在你有一个问题,因为当你读一段代码,你看到一个变量被使用,你不能通过命名约定告诉这个变量是一个ivar或局部变量。这吸。解决方案是对于ivars和局部变量有不同的命名约定。这只是普通的常识。



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



最后,代码质量更重要






您所展示的代码:不要在字符串属性中使用保留。您应该使用复制



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



NSString属性:复制或保留?


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?

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;

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

EDIT:

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

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.

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.

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)

In the end, code quality is more important than following a stupid rule that isn't even followed by the people preaching it.


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 property: copy or retain?

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

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