防止“本地声明隐藏实例变量"的Objective-C约定警告 [英] Objective-C convention to prevent "local declaration hides instance variable" warning

查看:84
本文介绍了防止“本地声明隐藏实例变量"的Objective-C约定警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码...

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

  • "variableName"的本地声明隐藏实例变量
  • "comparisonValue"的本地声明隐藏了实例变量

是否有处理这些警告的通用或可接受的约定?

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.

推荐答案

不幸的是,没有防止这种错误的好方法".常见的模式是使用稍微愚蠢的参数名称,例如

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;
}

这篇关于防止“本地声明隐藏实例变量"的Objective-C约定警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆