简体   繁体   中英

Regarding @synthesize in iOS6+

Eg:

//Myclass.h
@property(nonatomic, strong) NSString *name;

//MyClass.m
//@synthesize name = _name

So I know that we don't need to use @synthesize any more from iOS6+. The compiler automatically creates getters and setters for me.

  1. But I don't understand is when to use self.name = @"Testing" and when to use _name = @"Testing" ?

  2. Should _name = @"Testing" be ever used?

  3. If yes, when? When should an iVAR be used at all?

  4. Also if i want to write my own getter and setter do i need to write @synthesize or can i just write my getter and setter?

Thanks for your help!

Normally, you only use instance variables in init methods, getters and setters, and dealloc. There are exceptions of course, but this is a good rule of thumb.

If you write both your own getter and setter (or in the case of a readonly property and you write the getter), you have to synthesize your property yourself. In all other cases, the property is auto-synthesized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM