简体   繁体   中英

properties synthesize and iVars

Disclaimer: I'm pretty new to Objective-C

I have a couple of questions related to @property and @synthesize.

Let's say I have property

@property (strong, nonatomic) NSString *name;

1- I know that on Xcode 4.4+ you can skip doing @synthesize for the properties as they are automatically done. So I assume that the iVar generated should be the same as the name of the property right? In general, If I do @synthesize name; the iVar would be the same as the name of the property?

2- I know that @synthesize name = _name; is used when I have manually declared a private variable with the name of _name and I want to make the compiler use it as my property's iVar . I need to know, what happens if I do @synthesize name = _name; WITHOUT actually having a private variable declared.

Thank you in advance.

  1. If I'm not mistaken, the default iVars that are generated start with an underscore. Ie: the default iVar for the property name would be _name .
  2. The iVar _name will be generated for the property name . No need to manually create any iVar when @synthesize does the job for you, though there won't be any issues if you do want to manually add them.

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