0

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.

NarbehM
  • 345
  • 2
  • 13
  • Check this out http://stackoverflow.com/questions/14455577/objective-c-should-i-assign-the-variable-and-create-a-property-or-is-just-one-o –  Jan 31 '13 at 15:08

1 Answers1

1
  1. If I'm not mistaken, the default iVars that are generated start with an underscore. I.e.: 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.
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • Yes, in Xcode 4.4+ you only need `@property (nonatomic, strong) NSString *name;`. This will set up the property, accessors and ivars for you. The only time you need to actually use @synthesize is if you are overriding both the getter AND the setter. – Fogmeister Jan 31 '13 at 15:03
  • As far as I know, the iVar that is generated has the same name as the property, the distinction between the two is if you use `.` notation to access it. ie the difference between `myVar = value` and `self.myVar = value` – Dan F Jan 31 '13 at 15:05
  • @DanF the ivar produced is the same name as the property with an underscore before it. i.e. `self.someValue == _someValue` – Fogmeister Jan 31 '13 at 15:09
  • In my code, if I try to access something that is named as one of my synthesized properties with an underscore before it, I get an unrecognized identifier error – Dan F Jan 31 '13 at 15:30