Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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