覆盖设置器方法并获取"xxx的本地声明隐藏实例变量". [英] Override setter method and get "Local declaration of xxx hides instance variable"

查看:102
本文介绍了覆盖设置器方法并获取"xxx的本地声明隐藏实例变量".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数变量,例如timeSignature,在.h文件中声明了它,并合成了一对setter/getter方法:

I have a integer variable, for example, timeSignature, declared it in .h file, and synthesized a pair of setter/getter methods:

在.h中:

@interface Metronome : NSObject {
    int timeSignature;
}
@property (nonatomic) int timeSignature;

在.m中:

@synthesize timeSignature;

我希望重写setter方法:当用户为其设置一个新值时,它会执行其他操作以及更改为新值:

I wish to override the setter method: when user set a new value to it, it does something else as well as changing to new value:

- (void) setTimeSignature:(int)timeSignature {
    self.timeSignature = timeSignature; //hides instance variable warning at this line
    [self doesSomethingElse];
}

这就是问题所在,局部变量和实例变量具有相同的名称.

And heres the problem, the local variable and the instance variable have the same name.

如何避免这种情况?

推荐答案

命名您的ivar _timeSignature并命名为@synthesize timeSignature = _timeSignature;

Name your ivar _timeSignature and make @synthesize timeSignature = _timeSignature;

这篇关于覆盖设置器方法并获取"xxx的本地声明隐藏实例变量".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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