xcode 覆盖setter方法并获得“xxx的本地声明隐藏实例变量”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10311169/
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
Override setter method and get "Local declaration of xxx hides instance variable"
提问by Ben Lu
I have a integer variable, for example, timeSignature, declared it in .h file, and synthesized a pair of setter/getter methods:
我有一个整数变量,例如timeSignature,在.h文件中声明,并合成了一对setter/getter方法:
in .h:
在.h:
@interface Metronome : NSObject {
int timeSignature;
}
@property (nonatomic) int timeSignature;
in .m:
在.m:
@synthesize timeSignature;
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:
我希望覆盖 setter 方法:当用户为其设置新值时,它会执行其他操作并更改为新值:
- (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.
问题来了,局部变量和实例变量同名。
How can I avoid this?
我怎样才能避免这种情况?
回答by jrturton
Rename the argument variable name:
重命名参数变量名称:
- (void) setTimeSignature:(int)newTimeSignature {
timeSignature = newTimeSignature; //hides instance variable warning at this line
[self doesSomethingElse];
}
Also, don't use the property from within the setter, this will create an infinite loop! self.timeSignature = will call setTimeSignature!
另外,不要使用 setter 中的属性,这会造成无限循环!self.timeSignature = 将调用 setTimeSignature!
This is one of the reasons people use underscore names (_timeSignature) as the backing ivar for properties.
这是人们使用下划线名称 (_timeSignature) 作为属性的支持 ivar 的原因之一。
回答by Roman Temchenko
Name your ivar _timeSignature
and make @synthesize timeSignature = _timeSignature;
命名你的 ivar_timeSignature
并制作@synthesize timeSignature = _timeSignature;
回答by JeremyP
Rename the parameter. I use aTimeSignature
or newTimeSignature
.
重命名参数。我使用aTimeSignature
或newTimeSignature
。
Also, and this is a bigger problem:
另外,这是一个更大的问题:
self.timeSignature = someVar;
is merely syntactic sugar for
只是语法糖
[self setTimeSignature: somevar];
The dot notation is misleading you into thinking the property is different from the setter. This is not true, the property isthe setter and getter. What you really have is this:
点符号会误导您认为该属性与 setter 不同。这不是真的,属性是setter 和 getter。你真正拥有的是:
- (void) setTimeSignature:(int)timeSignature {
[self setTimeSignature: timeSignature]; //hides instance variable warning at this line
[self doesSomethingElse];
}
You should be able to see the obvious difficulty once you change the notation.
更改符号后,您应该能够看到明显的困难。
If you are new to Objective-C, you should really avoid dot notation until you are thoroughly confident you understand exactly what it does (this is the advice of the Big Nerd Ranch guide to iOS programming).
如果您是 Objective-C 的新手,您应该真正避免使用点表示法,直到您完全确信自己完全了解它的作用(这是 Big Nerd Ranch iOS 编程指南的建议)。
回答by Vladimir
Simply renaming method parameter should fix the issue. Also your code produces infinite recursion as you actually call your setter method again - you need to remove property call as well:
只需重命名方法参数即可解决问题。此外,当您再次实际调用 setter 方法时,您的代码会产生无限递归 - 您还需要删除属性调用:
- (void) setTimeSignature:(int)timeSignature_ {
timeSignature = timeSignature_; //hides instance variable warning at this line
[self doesSomethingElse];
}