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 Blog Reader iPhone App Data Modeling Creating a Custom Class: Part 1

Setters

Hello,

Does anyone know why there needs to be two different objects for the setter's implementation ( "title" and "_title" )?

Thanks! Justin

1 Answer

Mike Baxter
Mike Baxter
4,442 Points

Hi Justin,

The reason is that there are actually two variables in the example, and unless you name them different from one another, you wind up with confusion in knowing which one you're referring to. That's why in the video he goes back to add the underscore to make the second variable have a different name. So he had two variables named "title", but he renamed one of them to "_title" with the underscore. That clears up the confusion the compiler will have.

One thing to keep in mind, the way the video does the naming scheme is actually kind of bad; it's backwards from what a lot of developers are doing. The common practice is to make your instance variables have the underscores, and to not add underscores to your local variables. (In case you didn't catch that, an instance variable is a variable you can access from anywhere inside the class because it gets declared in the "interface" or header area/file. There are also local variables, which only have a scope of their little tiny neighborhood of whatever function you're working in, meaning you can't refer to them everywhere in your class's implementation.) Of course, you don't have to do it this way, and the Team Treehouse video doesn't. But it's a really good convention to follow, especially if you hope to work with other programmers some day.

I hope this helps!

Very good! Thank you Mike this really helped.

Mike Baxter
Mike Baxter
4,442 Points

Of course, glad I can help!