本地声明隐藏实例变量警告

时间:2011-12-12 06:06:36

标签: iphone ios xcode

本地声明隐藏“self.treatmentId = treatmentId;”附近的实例变量消息弹出窗口和“self.treatmentName = treatmentName;”;

@implementation Treatment

@synthesize treatmentId;
@synthesize treatmentName;

-(Treatment *)initWithtreatmentName:(NSString *)treatmentName treatmentId:(NSString *)treatmentId{


    if((self = [super init])){
        self.treatmentId = treatmentId;
        self.treatmentName = treatmentName;
    }

    return self;

 }

@end

2 个答案:

答案 0 :(得分:9)

将您的代码更改为以下内容。

@implementation Treatment

@synthesize treatmentId;
@synthesize treatmentName;

-(Treatment *)initWithtreatmentName:(NSString *)newTreatmentName treatmentId:(NSString  *)newTreatmentId{


    if((self = [super init])){
        self.treatmentId = newTreatmentId;
        self.treatmentName = newTreatmentName;
    }

    return self;

 }

@end

通过声明局部变量trentmentNametreatmentId,您实际上失去了通过其名称访问全局变量(iVars)的能力。

答案 1 :(得分:0)

此错误表示先前已声明self.treatmentName和self.treatmentID,因此本地声明隐藏了实例变量。如果您只是将init方法更改为:

-(Treatment *)initWithtreatmentName:(NSString *)name treatmentId:(NSString *)identifiction

并且你实现的方法反映了这一点,你会发现错误应该消失。