Obj-c中self.instanceVar = X和instanceVar = X之间的差异 [英] Difference between self.instanceVar = X and instanceVar = X in Obj-c

查看:95
本文介绍了Obj-c中self.instanceVar = X和instanceVar = X之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于obj-c类中的以下两行:

For the following two lines in an obj-c class:

self.instanceVar = X
instanceVar = X 

'self'版本调用合成'setter'的唯一区别,而后者只是设置instanceVar并且不通过setter?

Is the only difference that the 'self' version calls the synthesized 'setter', whereas the latter simply sets the instanceVar and doesn't go through the setter?

谢谢

推荐答案

想象一下:

@property( retain ) NSString * myprop;

如果您按 self.myprop 设置它,NSString实例将被保留。

If you set it by self.myprop, the NSString instance will be retained.

如果直接设置实例变量,情况就不是这样了。

If you set directly the instance variable, this will not be the case.

所以总是使用 self。,除非你绝对确定...

So always use the self., unless you're absolutely sure...

这篇关于Obj-c中self.instanceVar = X和instanceVar = X之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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