Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

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

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];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...