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.
If your method truly is an initialiser, don't forget to do your
self = [super init];
.I have never personally encountered a situation where
self
has changed tonil
or another value, but it's the Objective-C Initialiser Idiom™.This is not a problem at all in the modern Objective-C. In the modern Objective-C, properties are automatically synthesized and their corresponding instance variables get a
_
prefix.So, with auto-synthesis your properties would create instance variables
_variableName
and_comparisonValue
. No shadowing occurs in this case.More info in this blog post
If you absolutely need to manually synthesize your properties, rename the sinthesized ivar like this
In general case, rename your method arguments.
Unfortunately, no there's no "good" way to prevent this error. The common pattern is to use a slightly stupid parameter name like
You can use just this but without the
@synthesize
- whenever you want to use that variable you just write_*variable name*
and it eliminates the errorThough it's old question but still I've a good solution to suppress warning in code
You can learn about GCC pragma here and to get the warning code of a warning go to the Log Navigator (Command+7), select the topmost build, expand the log (the '=' button on the right), and scroll to the bottom and there your warning code is within square brackets like this
[-Wshadow-ivar]
Edit
For clang you can use
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.