Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App with Objective-C Creating a Data Model Refactoring Into a Model

Why not always use the _property keyword instead of using self.property?

As I wrote in the title, why it's a common pratice to use self.property instead use _property, that's always ready to use, unlike the self.property notation?

1 Answer

So in Objective-C you had something like this:

@property (nonatomic, strong) NSString *someString;

You could access someString by doing self.someString or _someString

self.someString` is the recommended way of doing it since it passes through KVO and calls the correct setters / getters (if you were to override those)

_someString` would just set the variable

So an @property was a bit magic because it would create two methods that you woudln't see:

  • (void)setSomeString:(NSString *)someString {}

and -(NSString *)someString`

You can read up on all this here