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 (iOS7) Refactoring into a Model Readonly Attribute

Underscore on predictions

Hey,

Here we use

  • (NSArray *) predictions{ if (_predictions == nil){ _predictions ... etc

Why the underscore before predictions? I don't understand that.

Thanks a lot, awesome course.

2 Answers

Stone Preston
Stone Preston
42,016 Points

using _variableName accesses an instance variable directly, without using getter and setter methods. When you create a property (such as your predictions array property) an instance variable is created behind the scenes that has the form of _variableName. you can access that directly using _variableName, or you can use getter and setter methods that were created as well (variableName and setVariableName are the getter and setter methods which can be called using self.variableName or [self variableName] and [self setVariableName] ).

When you write your own custom getter and setter methods (you are writing a custom getter in the case of your predictions method) , you need to reference the instance variable directly so you must use _predictions. you dont want to access the instance variable using the getter/setter methods if you are inside the getter/setter method itself.

I got it.

Thanks!