Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

property not working with getter AND setter

Tags:

properties

ios

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.

like image 359
Fogmeister Avatar asked Dec 11 '12 09:12

Fogmeister


1 Answers

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

like image 69
Alladinian Avatar answered Oct 05 '22 22:10

Alladinian