用点或括号语法设置属性之间有区别吗? [英] Is there a difference between setting a property with the dot or the bracket syntax?
问题描述
鉴于下面的属性声明,方法(A)的工作方式与方法(B)完全相同吗?我只想检查self.yellowViewController = yellcon_New;
正在通过我的设置器,以便释放旧对象并保留新对象.
Given the property declaration below, does method (A) work in exactly the same way as method (B)? I just want to check that self.yellowViewController = yellcon_New;
is going via my setter, so that the old objects gets released and the new one retained.
// INTERFACE
@property(nonatomic, retain) YellowViewController *yellowViewController;
// IMPLEMENTATION (A)
self.yellowViewController = yellcon_New;
// IMPLEMENTATION (B)
[self setYellowViewController:yellcon_New];
推荐答案
所有这些都是正确的:
self.yellowViewController = yellcon_New;
还有
[self setYellowViewController:yellcon_New];
工作相同. 我想添加一些有趣的东西:当您使用
Work the same. I would like to add something interesting : when you use
yellowViewController = yellcon_New;
您可以直接将值关联到ivar,而无需通过设置方法.
you associate directly the value to the ivar, without going through your setter methode.
如果有的话
-(void)setYellowViewController:(YellowViewController*)theYellowViewController;
{
NSLog(@"Setting the yellow view controller");
[yourWife askFor:beer];
...whatever...
...set the yellowViewController (retain in your case)
}
通话
self.yellowViewController = yellcon_New;
和
[self setYellowViewController:yellcon_New];
将使用setter方法(并记录消息,并让您的妻子为您带来一些啤酒)
will use the setter method (and log the message, and make your wife bring you some beer)
但是
yellowViewController = yellcon_New;
不会.
在某些情况下知道这一点很有趣.
It's interesting to know this in some cases.
这篇关于用点或括号语法设置属性之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!