-2

Possible Duplicates:
How does an underscore in front of a variable in a cocoa objective-c class work?
Prefixing property names with an underscore in Objective C

Why do a lot of Objective-C programmers put underscores before instance variables (even if they never even access them directly anywhere from their code)? What's the logic behind that?

Community
  • 1
  • 1
tux91
  • 1,674
  • 10
  • 16
  • I mainly use it for organization. – esqew Aug 18 '11 at 21:08
  • More possible duplicates: [Naming convention in Objective C /C , start with "_"?](http://stackoverflow.com/questions/2660618/naming-convention-in-objective-c-c-start-with); [Why do Cocoa-Touch class ivars have leading underscore character?](http://stackoverflow.com/questions/3544067/why-do-cocoa-touch-class-ivars-have-leading-underscore-character); [Synthesized property and variable with underscore prefix: what does this mean?](http://stackoverflow.com/questions/6049269/synthesized-property-and-variable-with-underscore-prefix-what-does-this-mean) – zpasternack Aug 18 '11 at 21:52

2 Answers2

2

Prepending an underscore to instance variable names is a convention for all variables that aren't meant to be accessed outside of the class instance. This makes it clear to the developer (and the user of the code if it's in a library) what's intended to be accessed externally and what isn't.

yan
  • 20,644
  • 3
  • 38
  • 48
1

The logic is to make it clear when accessing the property vs accessing the ivar.

You can't mistakenly access the raw ivar without the property if its underscored. I don't do this in my own code though.

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90