为什么Xcode自动创建带下划线的变量? [英] Why does Xcode automatically create variables with underscores?

查看:184
本文介绍了为什么Xcode自动创建带下划线的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在最新版本的Xcode(dp-4)中,使用retain,nonatomic声明的变量必须在变量名之前使用下划线?这会创建某种类型的安全性吗?

Why in the newest version of Xcode (dp-4) are variables declared with retain,nonatomic made to use the underscore before the variable name? Does this create some sort of type safety?

例如,我创建一个属性:

For example, I create a property:

@property (retain, nonatomic) IBOutlet UILabel *name;

除非我将dealloc中的变量更改为不具有_,否则我必须这样做:

Unless I change the variable inside the dealloc to not have the _, I have to do:

@synthesize name = _name;

这是为什么?

推荐答案

比我聪明的马克·达里姆普(Mark Dalrymple)写了

Mark Dalrymple, who's way smarter than I am, wrote a blog post at Big Nerd Ranch about this very subject. Bottom line: the underscore is a good idea. I will summarize his post here just in case the link stops working in the future, but if possible you should read his post instead of my summary.

当必须明确调用@synthesize时,他将帖子写回了.他主张使用以下代码:

He wrote this post back when explicitly calling @synthesize was mandatory. He advocated code such as:

// soapbubble.m
@synthesize viscosity = _viscosity;
@synthesize detergentBrand = _detergentBrand;

这些天,Xcode会自动隐式包含@synthesize.而且它使用了下划线,因此显然苹果的工程师同意Mark的观点.

These days Xcode automatically and implicitly includes @synthesize. And it does so using the prepended underscore so apparently Apple's engineers agree with Mark.

他的第一个原因是文体.它使您可以轻松查看哪些变量是局部变量,哪些是setter中的参数:

His first reason is stylistic. It allows you to easily see which variables are local and which are arguments in a setter:

- (void) setPonyName: (NSString *) ponyName {
    [_ponyName autorelease];
    _ponyName = [ponyName copy];
}

(这是ARC之前的设置程序,因此现在完全不需要这种方法,但是如果设置程序除了简单地释放和分配值之外,所做的工作还更多.)

(This is a pre-ARC setter, so now this method would be completely unnecessary, but if the setter did anything more involved than simply releasing and assigning a value it would still apply.)

他的第二个原因(也是我认为更重要的一个原因)是消除了一类很难追踪的错误.

His second reason (and the one I think is more important) is that eliminates a certain class of bug that can be very difficult to track down.

此代码:

self.ponyName = @"Mikey";

等同于:

[self setPonyName: @"Mikey"];

在没有下划线的情况下,此代码也有效:

Without the prepended underscore, this code is also valid:

ponyName = @"Mikey";

但它不会调用设置器,因此不会出现设置器中的任何副作用.同样,在设置器除了更改局部变量的值之外还要做其他工作的情况下,这可能会引起很大的麻烦.使用前置的下划线,该行将导致编译错误.您必须非常明确地想要设置局部变量:

but it doesn't call the setter so any side effects in the setter don't occur. Again, in a situation where the setter does extra work besides changing the local variable's value this can cause big headaches. With the prepended underscore, that line would cause a compile error. You would have to be very explicit about wanting to set a local variable:

_ponyName = @"Mikey";

并且,作为一名认真的程序员,您将包括一条注释,以准确解释为什么执行此不规则操作.

and, being the conscientious programmer you are, you would include a comment explaining exactly why you are performing this irregular maneuver.

这篇关于为什么Xcode自动创建带下划线的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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