Is there a difference between setting a property with the dot or the bracket syntax?

寵の児 提交于 2019-11-29 11:12:04

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!