This is a purely theoretical question:
I have a class that has a variable: varX.
I have a method that changes this variable. Xcode, with autocompletion suggest:
-(void)setVarX:(float)varX;
In implementation, when I write the instance method, Xcode tells me a warning:
“Local declaration of ‘varX’ Hides instance variable”
the method:
-(void)setVarX:(float)varX {
varX = varX;
}
So, to solve, I used the underscore in synthesize;
@synthesize varX = _varX;
and the method is:
-(void)setVarX:(float)varX {
_varX = varX;
}
is proper to use the underscore before variables in this way? otherwise how do I use the name of the method suggested by Xcode?
thanks
Xcode 4.4 (and later) have automatic synthesis of properties (so that
@synthesizeis not needed anymore) when you don’t use@dynamic. The automatic synthesis uses the underscore, so it seems that Apple wants this to be a convention.First, I disliked this idea, but now I see why it’s handy. Those variable names of those properties aren’t “reserved” anymore in more methods (I never use underscores in other situations).