-1

I've been running though some tutorials and often times I will see something like this.

in the .h

UIButton *_loginButton;

@property (retain) IBOutlet UIButton *loginButton;

Then in the .m it will be something like...

@synthesize loginButton = _loginButton;

So my question is what benefit does putting an _ before do? or why is it common practice? and lastly should I be doing this as well?

James Dunay
  • 2,714
  • 8
  • 41
  • 66
  • 1
    possible duplicate [here](http://stackoverflow.com/q/8545363/971401) –  Jan 19 '12 at 14:00
  • duplicate of [How does an underscore in front of a variable in a cocoa objective-c class work?](http://stackoverflow.com/q/822487/) and http://stackoverflow.com/q/5582448/ http://stackoverflow.com/q/2371489/ and http://stackoverflow.com/q/5466496 and http://stackoverflow.com/q/3521254/ and http://stackoverflow.com/q/7174277/, [among others](http://stackoverflow.com/search?q=%5Bobjc%5D+synthesize+underscore&submit=search). – jscs Jan 19 '12 at 18:05

3 Answers3

4

It's a convention that people sometimes use to denote private instance variables, or to separate instance variables from properties. You'll sometimes see names with a leading underscore used for private functions or methods. As far as I know, Apple reserves that convention for its own use and recommends that you not use it in order to avoid name collisions. For that reason, you'll sometimes also see names that have a trailing underscore instead of a leading one, i.e. foo_ instead of _foo.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • +1. I guess Apple "reserving" the leading underscore is pretty much the same as with C functions: a leading underscore is reserved for the system provider (C library), double underscore is for the compiler. That's why I personally prefer the trailing underscore. – DarkDust Jan 21 '12 at 23:36
0

Because its a local variable.

Some companies have internal requirements to write the local variables with _

Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33
  • 1
    Its not just for companies, though. Sometimes, especially with constructors, it reduces naming conflicts with variables passed to a function. Thats why all my private variables are named `varaiableName_`, or I simply use properties. – Richard J. Ross III Jan 19 '12 at 14:01
0

I don't know which version this started with, but I've noticed that you don't need to have a actual variable declaration at all if you're just going to represent it as a property.

// Interface
@property (retain) IBOutlet UIButton *loginButton;

// Implementation
@synthesize loginButton

And then you can just use the property:

loginButton = (....)
self.loginButton = (...)
[self setLoginButton:(...)]
etc...

Now, whether one way or another is The Right Way depends on personal taste really. For something like this, it usually always falls in the "stay consistent and following whatever coding standards are in place by you (sole developer) or your company/group/team)" category.

Anthony
  • 9,451
  • 9
  • 45
  • 72