13

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.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 3
    Do you have `_eventTypes` defined in your class? As well as the property you'll need to declare the variable in the header file. Also (and I'm not 100% sure on this) you may need to use `@synthesize` on that property even if you're writing your own getter and setter (that way, your getter and setter override the synthesized ones). – benwad Dec 11 '12 at 09:58
  • 3
    `@synthesize eventTypes = _eventTypes;` will do the trick – Alladinian Dec 11 '12 at 09:59
  • 1
    You no longer need to `@synthesize` properties as of Xcode 4.5. Having said that, I added `@synthesize` and it worked! Weird! Thanks – Fogmeister Dec 11 '12 at 10:02
  • 1
    Whoever adds the answer first will get accepted :D They both helped so thank you. – Fogmeister Dec 11 '12 at 10:03

4 Answers4

20

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

Alladinian
  • 34,483
  • 6
  • 89
  • 91
1

Add this in @implementation

@synthesize eventTypes = _eventTypes;
DD_
  • 7,230
  • 11
  • 38
  • 59
1

You have to @synthesize your property. We use it when we wanna create a custom getters and setters, like:

  • Read/Write property with custom getter and setter;
  • ReadOnly property with custom getter;

Right after your @implementation in .m file, add:

@synthesize propertyName = _propertyName;

This tell to compiler you want to get the control of the methods 'get' and 'set' of that specific property. So after that your implementation of set and get will work fine.


Extra explanations:

self.anyString = @"TEXT"; is exactly equivalent [self setAnyString:@"TEXT"]; Both of these are calling the method set. If you use _anyString you are accessing directly the property without pass through the set method.

So because of that, if we call a self.property inside of set method we will create a infinite loop. Once we're rewriting the methods 'set' and 'get' We have to use _ instead of self..

J. Lopes
  • 1,336
  • 16
  • 27
  • This question was answered 4 years ago and your answer is pretty much the same as the accepted answer. – Fogmeister Apr 27 '16 at 13:44
  • 2
    I came here from a newest question that was pretty similiar with yours. I've just seen that the guy put a link. Anyway sorry if I bothered you, I will leave my answer if someone doesn't see the link and read here a little explanation. Thanks for your comment. – J. Lopes Apr 27 '16 at 15:58
-8

Try This one :

- (void)setEventTypes:(NSArray *)eventTypes
{
    self.eventTypes = eventTypes;

    //do some stuff here.
}
- (NSArray*)eventTypes
{
    //do some stuff here.

    return self.eventTypes;
}
mmorris
  • 4,006
  • 3
  • 27
  • 29
Dipak_IOS
  • 156
  • 2