tags:

views:

350

answers:

4

I am using the following code ...

-(id) initWithVariableName:(NSString*)variableName withComparisonValue:(NSString*)comparisonValue {

    // super init
    self = [super init];
    if (!self) return nil;

    // set instance variables
    self.mustExist = NO;
    self.reverseCondition = NO;
    self.regularExpression = NO;
    self.variableName = variableName; // generates warning
    self.comparisonValue = comparisonValue; // generates warning

    return self;
}

which generated the following two warnings ...

  • Local declaration of 'variableName' hides instance variable
  • Local declaration of 'comparisonValue' hides instance variable

Is there a common or accepted convention for dealing with these warnings?

I understand that it is simply to inform the user that they should specify an instance when referring to the class member, but its annoying.

+3  A: 

Unfortunately, no there's no "good" way to prevent this error. The common pattern is to use a slightly stupid parameter name like

-(id) initWithVariableName:(NSString*)theVariableName 
       withComparisonValue:(NSString*)theComparisonValue {
    self.variableName = theVariableName;
    self.comparisonValue = theComparisonValue;

    return self;
}
Barry Wark
I prefer initialVariableName, myself.
codewarrior
@codewarrior Oooh, nice idea. I like that better as well. Apple docs often use `theX`, but `initialX` is more descriptive.
Barry Wark
It gets silly again when you have initialMiddleInitial etc.
codewarrior
A: 

You should generally prefix instance variables with something like an underscore (e.g. _variableName) to avoid compiler warnings like this.

Otherwise just slightly change the names in your method signature, there is no hard defined naming convention.

macatomy
Apple discourages using underscores for this, because they use this notation for their private instance variables.
Chuck
Good point, maybe a little more unique prefix would be nicer.
macatomy
@Chuck - Apple discourages using ivars with the initial underscore (or statically scoped variables), but automatic variables like this should be fine. Since you don't compile in the private headers, you won't get the warning, and since you're not (or should not be) accessing those ivars anyway, the automatic scope masking the ivar is not an issue.
Jason Coco
@Jason Coco: I don't know what you mean "automatic variables like this". The answer suggests prefixing *instance variables*, so I was just pointing out that an underscore might not be the best choice of prefix.
Chuck
@Chuck: You're absolutely right, I misread the answer. I thought he was prefixing the parameter names for some reason.
Jason Coco
+1  A: 

Either give the local a more descriptive name (e.g. initialVariableName) or give instance variables a different notation (e.g. myClass_variableName). I prefer the latter in most cases because it calls attention to when I'm using class internals rather than the proper interface.

Chuck
A: 

If your method truly is an initialiser, don't forget to do your self = [super init];.

- (id) initWith...
{
    self = [super init];
    if (!self) return nil;

    // do stuff

    return self;
}

I have never personally encountered a situation where self has changed to nil or another value, but it's the Objective-C Initialiser Idiom™.

dreamlax
I knew there was something like that to do ... but this was real quick "filler" code which hasn't really been fleshed out yet :) Thanks for the heads up though :)
Nippysaurus
I updated the code example with your feedback :)
Nippysaurus