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

Given the property declaration below, is method (A) exactly the same as method (B)? I just want to check that self.yellowViewController = yellcon_New; goes through my setter, so the old objects are freed, and the new one is saved.

 // INTERFACE @property(nonatomic, retain) YellowViewController *yellowViewController; // IMPLEMENTATION (A) self.yellowViewController = yellcon_New; // IMPLEMENTATION (B) [self setYellowViewController:yellcon_New]; 
+4
source share
3 answers

All this is correct:

 self.yellowViewController = yellcon_New; 

AND

 [self setYellowViewController:yellcon_New]; 

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

 yellowViewController = yellcon_New; 

you directly bind the value to ivar without going through your setter method.

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) } 

Call

 self.yellowViewController = yellcon_New; 

and

 [self setYellowViewController:yellcon_New]; 

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

but

 yellowViewController = yellcon_New; 

will not.

It is interesting to know this in some cases.

+5
source

Yes, lines A and B work the same

You can verify this by using @dynamic instead of @synthesize for this property and put the NSLog message in the implementation of the setter method.

+4
source

Yes. If you use the @synthesize property for this property, it creates the -setYellowViewController: method for you.

+1
source

Source: https://habr.com/ru/post/1300426/


All Articles