Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is underscore in Objective C?

In objective-c the convention is to use an underscore as a prefix for the instance variable name. So in your code when you call _someProperty from within an object, you are calling the instance variable directly.

What does an underscore before a variable 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.

Why do some variables have underscore in front?

The underscore prefix is meant as a hint to another programmer that a variable or method starting with a single underscore is intended for internal use.


If you use the underscore prefix for your ivars (which is nothing more than a common convention, but a useful one), then you need to do 1 extra thing so the auto-generated accessor (for the property) knows which ivar to use. Specifically, in your implementation file, your synthesize should look like this:

@synthesize missionName = _missionName;

More generically, this is:

@synthesize propertyName = _ivarName;

It's just a convention for readability, it doesn't do anything special to the compiler. You'll see people use it on private instance variables and method names. Apple actually recommends not using the underscore (if you're not being careful you could override something in your superclass), but you shouldn't feel bad about ignoring that advice. :)


The only useful purpose I have seen is to differentiate between local variables and member variables as stated above, but it is not a necessary convention. When paired with a @property, it increases verbosity of synthesize statements – @synthesize missionName = _missionName;, and is ugly everywhere.

Instead of using the underscore, just use descriptive variable names within methods that do not conflict. When they must conflict, the variable name within the method should suffer an underscore, not the member variable that may be used by multiple methods. The only common place this is useful is in a setter or in an init method. In addition, it will make the @synthesize statement more concise.

-(void)setMyString:(NSString*)_myString
{
    myString = _myString;
}

Edit: With the latest compiler feature of auto-synthesis, I now use underscore for the ivar (on the rare occasion that I need to use an ivar to match what auto-synthesis does.


It doesn't really mean anything, it's just a convention some people use to differentiate member variables from local variables.

As for the error, it sounds like aMission has the wrong type. What it its declaration?


This is only for the naming convention of synthesize properties.

When you synthesize variables in the .m file, Xcode will automatically provide you _variable intelligence.


Having an underscore not only makes it possible to resolve your ivars without resorting to using self.member syntax but it makes your code more readable since you know when a variable is an ivar (because of its underscore prefix) or a member argument (no underscore).

Example:

- (void) displayImage: (UIImage *) image {

    if (image != nil) {
        // Display the passed image...
        [_imageView setImage: image];
    } else {
        // fall back on the default image...
        [_imageView setImage: _image];
    }
}