Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you make an underscore in front of an instance variable? [duplicate]

Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?

I've seen this at Apple, inside UIPickerView.h:

id<UIPickerViewDataSource> _dataSource; 

why's that underscore there? Does it have a special meaning? A Convention I must know about?

like image 767
Thanks Avatar asked May 07 '09 23:05

Thanks


People also ask

Why put an underscore in front of a variable?

A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.

What does an underscore in front of a variable name mean?

Single leading underscore _var _ in front of a variable or method name is a weak internal use indicator. It warns the developer that this variable, method, or function is not supposed to be imported and used publicly.

Can a variable begin with an underscore?

Variable names can be arbitrarily long. They can contain both letters and digits, but they have to begin with a letter or an underscore.

Can a variable have an underscore in Java?

In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name.


2 Answers

A lot of people use this for private variables, to differentiate between private variables and public variables within an object.

It is a completely optional way of working.

like image 125
Reed Copsey Avatar answered Sep 20 '22 12:09

Reed Copsey


What you're seeing is the use of underlines to distinguish between instance variables and properties. So a class declaration might be:

@interface Foo {   NSString* _label;   .... }  @property (nonatomic, retain) NSString* label; // notice: no underline 

Then in the implementation file you would have:

@synthesize label=_label; // the property is matched with the ivar 

Now when inside the implementation, if you want to access the instance variable directly you could just use _label but to go through the property accessor methods (which take care of retain/releases and a bunch of other book-keeping tasks) you would use self.label. From the outside, you would always want to go through the {object}.label property.

The other way is to do without the underline and just use:

NSString* label; @property (nonatomic, retain) NSString* label; ... @synthesize label; 

It works the same but then it might confuse someone reading the code and trying to keep track of label vs self.label. I personally find the Apple convention (with underlines) a bit easier to read but it's a matter of preference.

like image 35
Ramin Avatar answered Sep 17 '22 12:09

Ramin