使用点或括号语法设置属性有区别吗?
鉴于下面的属性声明,方法 (A) 的工作方式是否与方法 (B) 完全相同?我只想检查 self.yellowViewController = yellcon_New;
是否通过我的 setter,以便旧对象被释放并保留新对象。
// INTERFACE
@property(nonatomic, retain) YellowViewController *yellowViewController;
// IMPLEMENTATION (A)
self.yellowViewController = yellcon_New;
// IMPLEMENTATION (B)
[self setYellowViewController: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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有这些都是正确的:
并且
工作原理相同。
我想添加一些有趣的内容:当您使用时,
您可以直接将值关联到 ivar,而无需通过 setter 方法。
因此,如果您有
Calling
并将
使用 setter 方法(并记录消息,并让您的妻子给您带来一些啤酒),
但
不会。
在某些情况下了解这一点很有趣。
All of this is correct :
And
Work the same.
I would like to add something interesting : when you use
you associate directly the value to the ivar, without going through your setter methode.
So if you have
Calling
and
will use the setter method (and log the message, and make your wife bring you some beer)
but
will not.
It's interesting to know this in some cases.
是的,A 和 B 行的工作原理相同,
您可以通过使用 @dynamic 而不是 @synthesize 来检查此属性,并将 NSLog 消息放入 setter 方法的实现中。
Yes, A and B lines work the same
You can check that by using @dynamic instead of @synthesize for this property and put NSLog message in you implementation of setter method.
是的。如果您为该属性使用
@synthesize
事物,它会为您创建一个-setYellowViewController:
方法。Yes. If you're using the
@synthesize
thing for that property, it's creating a-setYellowViewController:
method for you.