xcode 局部声明隐藏实例变量警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8470502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:43:50  来源:igfitidea点击:

local declaration hides instance variable warning

iphoneiosxcode

提问by susitha

local declaration hides instance variable message popup near "self.treatmentId = treatmentId;" and "self.treatmentName = treatmentName;";

本地声明隐藏了“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

回答by Littlejon

Change your code to the following.

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

@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

By declaring the local variable trentmentNameand treatmentIdyou are essentially losing the ability to access the global (iVars) via their names.

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

回答by Jamie

This error means that self.treatmentName and self.treatmentID have been declared previously so the local declaration is hiding the instance variables. If you simply change your init method to:

这个错误意味着 self.treatmentName 和 self.treatmentID 之前已经声明过,所以本地声明隐藏了实例变量。如果您只是将 init 方法更改为:

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

and your implementation of the method to reflect this, you'll find that the error should go away.

以及您对方法的实现以反映这一点,您会发现错误应该消失。