15

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.

Community
  • 1
  • 1
Java Ka Baby
  • 4,880
  • 11
  • 39
  • 51
  • upvoting because i feel we should familiar with all kinds of standard naming conventions. – amod Aug 24 '11 at 11:23

3 Answers3

9

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.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
8

Don't do it.

Single leading underscores are an Apple internal coding convention. They do it so that their ivar names won't collide with yours. If you want to use a prefix on your ivar names, use anything but a single underscore.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • I think some people also use it to avoid accidentally using raw ivars instead of accessors? – andyvn22 Aug 24 '11 at 11:59
  • Does `__variable` (two underscores) mean extra dangerous internal then? I was aware of the convention of internal methods but I've never seen these methods until I got deep into an Apple library – Allison Oct 11 '17 at 02:47
  • two underscore is fine. – Will Gwo Aug 06 '18 at 23:31
1

this is a naming convention normally used for c++ to define instance variable which are private

like in a class u may have

private:
int __x;

public:
int GetX()
{
  return this.__x;
}

this is a naming convention, i was forced to use in c++. However my teacher never told us the name of the naming convention. But i feel this is helpfull and readable specially when u are not using java naming conventions.

amod
  • 4,190
  • 10
  • 49
  • 75
  • Thank you nice exaplaination +1 – Java Ka Baby Aug 24 '11 at 20:07
  • Java and Objective-C are two different languages. Don't assume that just because you can do it Java, you should do it in Objective-C. – SevenBits Aug 22 '13 at 23:09
  • @SevenBits I guess you have not read my answer properly. I have mentioned that they normally make your variable readable. Secondly java developers normally follow camelCase notation to achieve readability. Please read answer properly before down voting it. – amod Aug 23 '13 at 04:03