Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why put underscore "_" before variable names in Objective C [duplicate]

Tags:

objective-c

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

In objective C I am seeing lots of code with a underscore before variable names e.g _someVariable

why is that? also how to you write accessors i.e get and set method for such a variable.

like image 719
Java Ka Baby Avatar asked Aug 24 '11 10:08

Java Ka Baby


People also ask

Why do variables start with _?

The underscore in variable names is completely optional. Many programmers use it to differentiate private variables - so instance variables will typically have an underscore prepended to the name. This prevents confusion with local variables.

What does an underscore before a variable name mean?

An underscore in front usually indicates an instance variable as opposed to a local variable. It's merely a coding style that can be omitted in favor of "speaking" variable names and small classes that don't do too many things. Follow this answer to receive notifications.

Can you use _ in variable names?

A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ )

Why should we avoid the use of the underscore to begin variable names with?

If you have many places where you are using _ as a convention then all of that will break. Using underscore in a variable like a_b is still valid. But using _ alone as variable name is no more valid.


1 Answers

The underscores are often used to show that the variables are instance variables. It is not really necessary, as ivars can have the same name as their properties and their accessors.

Example:

@interface MyClass : NSObject {
    NSString *_myIVar;                  // can be omitted, see rest of text
}
// accessors, first one is getter, second one is setter
- (NSString *) myIVar;                  // can be omitted, see rest of text
- (void) setMyIVar: (NSString *) value; // can be omitted, see rest of text

// other methods

@property (nonatomic, copy) NSString *myIVar;

@end

Now, instead of declaring and coding the accessors myIVar and setMyIVar: yourself, you can let the compiler do that. In newer versions, you don't even have to declare myIVar in the interface. You just declare the property and let the compiler synthesize the rest for you. In the .m file, you do:

@implementation MyClass
@synthesize myIVar; // generates methods myIVar and setMyIVar: for you, 
                    // with proper code.
                    // also generates the instance variable myIVar

// etc...

@end

Be sure to finalize the string:

- (void) dealloc {
    [myIVar release]; 
    [super dealloc];
}

FWIW, if you want to do more than the default implementation of the getter or setter do, you can still code one or both of them yourself, but then you'll have to take care of memory management too. In that case, the compiler will not generate that particular accessor anymore (but if only one is done manually, the other will still be generated).

You access the properties as

myString = self.myIVar;

or, from another class:

theString = otherClass.myIVar;

and

otherClass.myIVar = @"Hello, world!";

In MyClass, if you omit self., you get the bare ivar. This should generally only be used in the initializers and in dealloc.

like image 147
Rudy Velthuis Avatar answered Oct 20 '22 04:10

Rudy Velthuis