(目标C)执行@synthesize myvar = _myvar(如果有)有什么好处? [英] (Objective C) what is the advantage of doing @synthesize myvar = _myvar (if any)?

查看:79
本文介绍了(目标C)执行@synthesize myvar = _myvar(如果有)有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
可可Objective-C类中变量前面的下划线如何工作?

Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?

除了对代码的可读性之外,我还不太清楚,为什么要在创建属性时创建带有下划线前缀的内部变量.

It is not fully clear to me (other than for readability of the code), why you wanna create an internal variable with an underscore prefix when you create the property.

由于所有事情都是在内部处理的,为什么不这样做,因为我们没有在getter和setter中添加任何代码?

Since everything is handled internally, why bother to do so, since we do not add any code to the getter and setter?

即使我必须向getter或setter添加一些代码,我也看不到为什么我不能只对myvar进行检查,而不必检查_myvar然后将其分配给myvar.

And even if i gotta add some code to the getter or setter, i do not see why i cannot just do the check on myvar instead than having to check _myvar and then assign it to myvar.

除了因为每个人都这样做是因为吗?"之外,有人可以给我一些解释吗?我想了解这种做法背后的全部原因(即使没有针对getter和setter的自定义代码,这似乎很常见).

Can anyone give me some explanation, other than "do it because that's what everyone does ?" I would like to understand the whole reason behind this practice (that seems to be pretty common even if there is no custom code for the getter and setter).

谢谢!

推荐答案

Objective-C属性通常具有支持的实例变量(我想您知道属性和实例变量之间的区别).

An Objective-C property usually has a backing instance variable (I guess you know the difference between a property and an instance variable).

该属性的名称可能与实例变量的名称不同.

The property may have a different name than the instance variable.

例如,您可能有一个名为x的实例变量,具有一个名为y的属性.

For instance, you may have an instance variable named x, with a property named y.

您可以使用以下方法将y属性合成为x变量:

You can synthesize the y property to the x variable using:

@synthesize y = x;

现在谈下划线.

通常的做法是,在实例变量具有与实例变量同名的方法参数时,对实例变量使用下划线前缀,以防止命名冲突或编译器警告(阴影变量).

It's a common practice to use an underscore prefix for instance variables, to prevent naming collisions, or compiler warnings (shadowed variable), when having for instance a method argument with the same name as an instance variable.

下划线前缀还可以清楚地表明您是在引用实例变量.

The underscore prefix also makes clear that you are referring to an instance variable.

通过对实例变量使用下划线前缀,您可以在方法的参数,堆栈变量等中自由使用不带下划线的名称.

By using the underscore prefix for instance variables, you're free to use the name without the underscore in method's arguments, stack variables, etc.

但是在使用属性时,通常不希望用户写下划线.

But when using a property, you usually don't want the user to write an underscore.

因此,通常对于_x实例变量具有x属性.

So you usually have an x property for an _x instance variable.

这就是为什么你写:

@synthesize x = _x;

让我们举个例子:

@interface Test: NSObject
{
    int x;
}

@property( readonly ) int x;

@end

这很普遍...但是现在在实现中想象一下:

This is quite common... But now imagine this in the implementation:

- ( id )initWithX: ( int )x
{}

我们有一个命名冲突.

在我们的方法中,x将引用该方法的参数.而且没有访问x实例变量的漂亮方法.

Inside our method, x will refer to the method's argument. And there is no pretty way to access the x instance variable.

根据编译器的警告标志,这可能还会生成警告(-Wshadow).

Depending on your compiler's warning flags, this may also generate a warning (-Wshadow).

如果您为实例变量使用下划线前缀,则一切都非常简单:

If you use an underscore prefix for your instance variable, everything is just simple:

- ( id )initWithX: ( int )x
{
    if( ( self = [ super init ] ) )
    {
        _x = x;
    }

    return self;
}

没有冲突,没有命名冲突,阅读效果得到了改善……只是一种不错的方式...

No conflict, no naming collision, improved reading... Just a nice way...

这篇关于(目标C)执行@synthesize myvar = _myvar(如果有)有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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