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

Soojin Ro
Soojin Ro
13,331 Points

[obj-c] underscore & self notation difference?

sometimes, I see _objectName and sometimes I see self.objectName.

What is the difference?!??

2 Answers

Nick Fuller
Nick Fuller
9,027 Points

I just had a long discussion with one of my iOS developers about this the other day!

I come from a heavy Ruby background where things are slightly different, but I'll give you my 2 cents.

When you have an object and you create a property, it is creating an instance variable for that property. In objective-c the convention is to use an underscore as a prefix for the instance variable name.

So in your code when you call _someProperty from within an object, you are calling the instance variable directly. This is not bad, but in my OPINION I would prefer to use the getter method [self.someProperty]. I prefer that for one simple reason, if I need to change the way the getter method handles the instance variable, then I only need to change it in one place, the method itself. Whereas if I use the instance variable directly and I need to change the way I get it, then I have to search through all of my code to find it and change it in a lot of places.

Now... again I'm from a more Ruby background of development and the world of Objective-C is quite similar, but still very different. My lead iOS developer, whom has been writing objective-c for over a decade, accesses his instance variables directly.

So really, it's a matter of preference and what your application requires. The most important thing to know is how the properties and instance variables work! Then you can see it either way. Hope that helps or makes sense

Soojin Ro
Soojin Ro
13,331 Points

Thank you so much for the explanation!

Liam Brady
Liam Brady
9,713 Points

I'm still learning Objective-C and iOS, so this is by no means a definitive answer...

I was confused by this a few days ago and this is what I found out:

Let's use a myName @property as an example

First we declare it:

@property(nonatomic) NSString *myName;

Basically _myName = @"Soojin" directly sets the value of the @property myName. If you use self.myName = @"Soojin", this calls a setter method which looks like this

Implementation (.m) file (hidden)

- (void)setMyName:(NSString *)myName {
   _myName = myName;

// If your input was @"Soojin", this would look like the code below
// _myName = @"Soojin";

}

The best way I can see to describe this is that self.myName is technically exactly the same as _myName, except self.myName calls a method whose sole purpose is to perform _myName = @"Soojin" for you instead of you directly accessing your myName @property by doing _myName = @"Soojin" yourself.

Nick Fuller
Nick Fuller
9,027 Points

You're correct! Well said Liam!

Soojin Ro
Soojin Ro
13,331 Points

Thank you so so much I totally understood!

Liam Brady
Liam Brady
9,713 Points

It took me a ridiculously long time to grasp this!

To finally get it, I went back to the basics of getters and setters. Then most recently, Sam Soffes Photo Bombers app tutorial helped me with this. He overrides a setter method and adds some extra things to it. This kind of brought it home to me that until you add these extra things it's only purpose is to directly access a @property itself. As you said, I guess like many things, it comes down to preference and probably confidence too.

Soojin Ro
Soojin Ro
13,331 Points

Thanks again. I first learned programming with Java and i guess this is one of so many differences. But I'm really starting to like iOS development