Objective-C convention to prevent “local declarati

2020-02-02 08:55发布

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.

标签: objective-c
9条回答
\"骚年 ilove
2楼-- · 2020-02-02 09:13

If you are using local instance variable name same as global instance variable name then this warning occur.

First Methode: For ignoring this warning use have to change local instance variable name or else change the global instance variable name.

Second Methode: if you want to use global variable then call as self->variableName

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

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

    // set instance variables
    self->variableName = variableName; //point to global variableName
    self->comparisonValue = comparisonValue; //point to global comparisonValue

    return self;
}
查看更多
在下西门庆
3楼-- · 2020-02-02 09:19

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.

查看更多
倾城 Initia
4楼-- · 2020-02-02 09:21

I see this is a fairly old question with a accepted answer but I have a better solution and its code convention.

The convention states that you prefix private variables with a underscore (_varName) and public (like properties) with just the name.

With this you just can call the same variable name in your functions.

Example:

ExampleClass.h

@interface ExampleClass : NSObject
{
    NSString *_varName; //this is not required if you create a property
}

@property (nonatomic, retain) NSString *varName;

- (void)someMethodWithVarName:(NSString *)varName;

@end

ExampleClass.m

#import "ExampleClass.h"

@implementation ExampleClass

@synthesize varName = _varName; //if you don't declare the _varName in the header file, Objective-C does it for you.

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)someMethodWithVarName:(NSString *)varName
{
    _varName = varName; //just for example purpose
}

@end
查看更多
登录 后发表回答