使用点或括号语法设置属性有区别吗?

发布于 2024-08-20 08:38:13 字数 376 浏览 13 评论 0原文

鉴于下面的属性声明,方法 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2024-08-27 08:38:13

所有这些都是正确的:

self.yellowViewController = yellcon_New;

并且

[self setYellowViewController:yellcon_New];

工作原理相同。
我想添加一些有趣的内容:当您使用时,

yellowViewController = yellcon_New;

您可以直接将值关联到 ivar,而无需通过 setter 方法。

因此,如果您有

-(void)setYellowViewController:(YellowViewController*)theYellowViewController;
{
    NSLog(@"Setting the yellow view controller");
    [yourWife askFor:beer];
    ...whatever...
    ...set the yellowViewController (retain in your case)
}

Calling

self.yellowViewController = yellcon_New;

并将

[self setYellowViewController:yellcon_New];

使用 setter 方法(并记录消息,并让您的妻子给您带来一些啤酒),

yellowViewController = yellcon_New;

不会。

在某些情况下了解这一点很有趣。

All of this is correct :

self.yellowViewController = yellcon_New;

And

[self setYellowViewController:yellcon_New];

Work the same.
I would like to add something interesting : when you use

yellowViewController = yellcon_New;

you associate directly the value to the ivar, without going through your setter methode.

So if you have

-(void)setYellowViewController:(YellowViewController*)theYellowViewController;
{
    NSLog(@"Setting the yellow view controller");
    [yourWife askFor:beer];
    ...whatever...
    ...set the yellowViewController (retain in your case)
}

Calling

self.yellowViewController = yellcon_New;

and

[self setYellowViewController:yellcon_New];

will use the setter method (and log the message, and make your wife bring you some beer)

but

yellowViewController = yellcon_New;

will not.

It's interesting to know this in some cases.

亣腦蒛氧 2024-08-27 08:38:13

是的,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.

故事与诗 2024-08-27 08:38:13

是的。如果您为该属性使用 @synthesize 事物,它会为您创建一个 -setYellowViewController: 方法。

Yes. If you're using the @synthesize thing for that property, it's creating a -setYellowViewController: method for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文