_和自我之间的区别。在Objective-C中 [英] Difference between _ and self. in Objective-C

查看:122
本文介绍了_和自我之间的区别。在Objective-C中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用 @property self 关键字是否有区别>?

Is there a difference between using the underscore and using the self keyword in Objective-C when calling an @property?

财产声明:

@property (weak, nonatomic) NSString *myString;

在物业上拨打 @synthesize

@synthesize myString = _myString;

如果我想在我的代码中使用它,是否有区别?什么时候?在getter / setter中?

Is there a difference if I want to use it in my code? When? In the getter/setter?

self.myString = @"test";
_myString = @"test";


推荐答案

self.myString = @ test; 完全等同于编写 [self setMyString:@test]; 。这两个都在调用方法。

self.myString = @"test"; is exactly equivalent to writing [self setMyString:@"test"];. Both of these are calling a method.

您可以自己编写该方法。它可能看起来像这样:

You could have written that method yourself. It might look something like this:

- (void)setMyString:(NSString*)newString
{
    _myString = newString;
}

因为您使用了 @synthesize ,你不必费心去编写那个方法,你可以让编译器为你编写它。

Because you used @synthesize, you don't have to actually bother writing that method, you can just allow the compiler to write it for you.

所以,从这个方法来看,它看起来像调用它将完成相同的事情只是为实例变量赋值,对吧?嗯,这不是那么简单。

So, from looking at that method, it looks like calling it will do the exact same thing as just assigning a value to the instance variable, right? Well, it's not so simple.

首先,你可以编写自己的setter方法。如果这样做,您的方法将被调用,它可以执行各种其他操作以及设置变量。在这种情况下,使用 self.myString = 会调用你的方法,但是 _myString = 不会,因此不同将使用功能。

Firstly, you could write your own setter method. If you do so, your method would get called, and it could do all sorts of additional things as well as setting the variable. In that case, using self.myString = would call your method, but doing _myString = would not, and thus different functionality would be used.

其次,如果您使用Key Value Observing,编译器会做一些非常聪明的技巧。在幕后,它是你的类的子类,并覆盖你的setter方法(无论是你自己编写的还是通过合成生成的方法),以便调用 willChangeValueForKey:密钥值观察工作所需要的。你不需要知道它是如何工作的(虽然如果你想睡前阅读它会很有趣!),但是你需要知道如果你想要Key Value Observing自动工作,你必须使用setter方法。

Secondly, if you ever use Key Value Observing, the compiler does some very clever tricks. Behind the scenes, it subclasses your class, and overrides your setter method (whether it's one you wrote yourself or one generated by synthesize), in order to make the calls to willChangeValueForKey: that are needed for Key Value Observing to work. You don't need to know how this works (although it's quite interesting if you want some bedtime reading!), but you do need to know that if you want Key Value Observing to work automatically, you have to use setter methods.

第三,即使您依靠合成来编写一个setter方法,也可以为将来提供灵活性。您可能希望在更改值时执行额外操作,并且在您发现要执行此操作时,您可以手动编写setter方法 - 如果您习惯于始终使用 self .myString = ,那么你不需要改变你的其余代码来开始调用新方法!

Thirdly, calling the setter method even if you're relying on synthesize to write one gives you flexibility for the future. You might want to do something extra whenever a value is changed, and at the point you discover you want to do that, you can manually write a setter method — if you're in the habit of always using self.myString =, then you won't need to change the rest of your code to start calling the new method!

第四,同样的适用于子类。如果其他人要对你的代码进行子类化,那么如果你使用setter,那么他们可以覆盖它们来调整功能。

Fourthly, the same applies to subclasses. If someone else was to subclass your code, if you use the setters then they could override them to adjust the functionality.

任何时候你直接访问实例变量,你'显然没有提供额外功能的方法。由于您或其他人可能希望在将来使用此类功能,因此除非有充分的理由不这样做,否则始终使用设置者是值得的。

Any time you access the instance variable directly, you're explicitly not providing a way for extra functionality to be hooked in at that point. Since you or someone else might want to hook in such functionality in the future, it pays to use the setters all the time, unless there's a good reason not to.

这篇关于_和自我之间的区别。在Objective-C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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