Objective-C convention to prevent “local declaration hides instance variable” warning

前端 未结 9 1368
滥情空心
滥情空心 2020-12-08 04:50

I am using the following code ...

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

    // super init
             


        
相关标签:
9条回答
  • 2020-12-08 05:16

    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™.

    0 讨论(0)
  • 2020-12-08 05:25

    Though it's old question but still I've a good solution to suppress warning in 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;
    
    
        #pragma GCC diagnostic push
        #pragma GCC diagnostic ignored "-Wshadow-ivar"
        self.variableName = variableName; // generates warning
        self.comparisonValue = comparisonValue; // generates warning
        #pragma GCC diagnostic pop
    
    
    
        return self;
    }
    

    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

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wshadow-ivar"
    // your code
    #pragma clang diagnostic pop
    
    0 讨论(0)
  • 2020-12-08 05:27

    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.

    0 讨论(0)
  • 2020-12-08 05:29

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 05:29
    _varName = varName;
    

    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 error

    0 讨论(0)
  • 2020-12-08 05:29

    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;
    }
    
    0 讨论(0)
提交回复
热议问题