不使用getter和setter的属性 [英] property not working with getter AND setter
问题描述
我已经定义了一个属性......
I have defined a property...
@property (nonatomic, strong) NSArray *eventTypes;
我想覆盖getter和setter ......
I want to override the getter and setter...
我写过这个......
I have written this...
- (void)setEventTypes:(NSArray *)eventTypes
{
_eventTypes = eventTypes;
//do some stuff here.
}
这种方法很好,但是当我添加这个...
This works fine but when I then add this...
- (NSArray*)eventTypes
{
//do some stuff here.
return _eventTypes;
}
然后这两个函数都显示错误,并且不知道_eventTypes是什么。
Then both of the functions show errors and don't know what _eventTypes is.
这两种方式都是一样的。它适用于一个功能但是只要我添加另一个功能它就会失败。
This is the same either way around. It works with one function but as soon as I add the other it fails both of them.
我还需要做些什么吗?似乎很奇怪,它可以与任何一个机器人一起工作,而不是两个。
Is there something else I need to do for this? Seems odd that it work with either one bot not both.
推荐答案
虽然LLVM将自动合成支持ivar(前缀为默认情况下是下划线),如果你同时实现了getter和setter方法,你将不获得一个自动合成的ivar,这就是为什么你必须 @synthesize eventTypes = _eventTypes ;
手动。
Although the LLVM will auto synthesize the backing ivar (prefixed by an underscore by default), in the case that you implement both the getter and setter methods you will not get an auto-synthesized ivar and this is why you must @synthesize eventTypes = _eventTypes;
manually.
您可以在此处阅读更多内容: http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html
You can read more on this here: http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html
这篇关于不使用getter和setter的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!