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

Shen JIE LIN
Shen JIE LIN
10,612 Points

Objective-C variables

Hi, so I am a little confused about objective-C variables, below is an example

in my .h file, I declared a property with the following line

@property (strong, nonatomic) NSArray *predictionsBackgroundColor;

then in my .m file, I alloc and initialize it with some UIColor objects that is all fine

but when I try to refer to it in a button pressed method, the compiler tells me that i need to refer to it as the following:

    self.predictionLabel.backgroundColor = _predictionsBackgroundColor[0];

what is the reasons for adding that extra "_" ??????

because it actually worked, and referred to the proper variable @_@

3 Answers

Stone Preston
Stone Preston
42,016 Points

when you create a property named propertyName, behind the scenes the compiler creates an instance variable named _propertyName. you can use _propertyName to access the property directly, or you can use self.propertyName to access the property through its getter and setter methods. Most of the time you will want to use the setter/getter methods so you refer to it as self.propertyName.

your code should have worked either way:

//access the instance variable directly
self.predictionLabel.backgroundColor = _predictionsBackgroundColor[0];
//access it using the getter/setter methods
self.predictionLabel.backgroundColor = self.predictionsBackgroundColor[0];
 Vladimir Cezar
 Vladimir Cezar
7,710 Points

To refer to a property you either user its accessor method:

self.propertyName

or the instance variable that is automatically created for you

_property

Here is a full explanation: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

The extra "_" means that you are directly accessing the instance variable that has been declared autmatically by the @property method. In the back-end of the @property method somewhere, this code is being initialized:

NSArray *_predictionsBackgroundColor;

If you feel the need to access the variable indirectly, the accessor method created by the @property statement does that for you

self.predictionsBackgroundColor[0]

As you may recall from one of the Objective-C videos here on Treehouse, adding "_" before a variable name is an indication to both yourself and others that the variable is an instance variable i.e. it is conventional