属性不适用于 getter 和 setter [英] property not working with getter AND setter

查看:25
本文介绍了属性不适用于 getter 和 setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个属性...

I have defined a property...

@property (nonatomic, strong) NSArray *eventTypes;

我想覆盖 getter 和 setter...

I want to override the getter and setter...

我已经写了这个...

- (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屋!

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