Take the 2-minute tour ×

*I'm not sure if StackOverflow is correct place for this question so please let me know if it is not.

In Apple's iOS 5 sample code (I don't think it appeared pre iOS 5) they have changed how they declare instance variables.

In the header file they no longer declare the variable and @property, they only do the @property.

e.g. instead of

     NSString *myVariable;

@property (strong, nonatomic) NSString *myVariable;

it's just:

@property (strong, nonatomic) NSString *myVariable;

In the implementation file they property is synthesized like this:

myVariable = _myVariable

And then throughout the code sometimes it is referred to as self.myVariable and other times _myVariable.

Could someone please explain to me the correct usage (when should it use self.myVariable vs. _myVariable

And why are variables no longer declared separately from @properties?

share|improve this question
SO would be the right place — if this wasn't asked already hundreds times before. –  vikingosegundo Jan 26 '12 at 4:36
Sorry, I did search before posting but obviously not thoroughly enough. –  kmcgrady Jan 26 '12 at 4:49

1 Answer 1

up vote 1 down vote accepted

The integration of properties with their backing variables was done likely for a number of reasons. I would imagine that chief among them were convenience, easier to read code (since things are in one place), and the desire to get people thinking in terms of properties, not variables. But this is all speculation on my part. (As you can probably gather, I'm a big fan of not having to doubly declare the property.)

You should generally use the accessor methods everywhere except where you need guarantees that the access will be side effect free. In practice, this means use accessor methods everywhere except -init... and -dealloc (and any methods called by them).

share|improve this answer
Thanks for the answer. The accessor method is self.variable right? –  kmcgrady Jan 26 '12 at 4:47
Yes, but this is why you usually give the backing variable a different name: to avoid confusion and let the compiler catch mistakes for you. –  Conrad Shultz Jan 26 '12 at 4:55
Great, thanks it all makes sense now. –  kmcgrady Jan 26 '12 at 4:58

Your Answer


