Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

ios - property not working with getter AND setter

I have defined a property...

@property (nonatomic, strong) NSArray *eventTypes;

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;
}

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

You can read more on this here: http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...