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 Objective-C Basics (Retired) Introduction to Objective-C From Structs to Objects

variable with underscore

it's not clear to me why the float variable in this lesson has underscore(_)?

4 Answers

Stone Preston
Stone Preston
42,016 Points

its an objective c naming convention.

when to add underscore in a variable? or it is just a naming convention to add underscore when variable is float only?

Stone Preston
Stone Preston
42,016 Points

whenever you want. It is a convention to differentiate member variables from local variables. You dont have to name your variables like that. Its just a convention that a lot of people use.

thank you so much sir Stone Preston, now I understand.

Stone Preston
Stone Preston
42,016 Points

you might also see it when using properties. If I have a property named variable I can either access it directly using _variable or use the setter/getter methods variable and setVariable. When using properties, the _variable is generated behind the scenes.

Stone Preston
Stone Preston
42,016 Points

assuming it was syntesized as @synthesize variable = _variable. But you dont have to do that kind of stuff nowadays. So what I said before may not be true

thanks again for added info.