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 Understanding @property

Underscore used with variables

I'm confused, when do you use an underscore before your variable name?

2 Answers

Robin Malhotra
Robin Malhotra
14,883 Points

It's a default way of referring to your ivars(instance variables for properties.

for example, @property (nonatomic,strong) NSInteger *x;

@synthesize x=_x;//default

In case you do not put in a @synthesize, Xcode automatically sets your ivar names to underscore+property name

If you're a little freaky with code, you can override by typing

@synthesize x=y

Guillaume Maka
PLUS
Guillaume Maka
Courses Plus Student 10,224 Points

Hi,

You use it in class methods implementation.

@implementation Object {
 NSString *_aString;
}
- (instancetype) init {
self = [super init];
 if(self){
  _aString = @"string";
 }
 return self;
}
@end