iosxcodeautomatic-propertiesivarsynthesize

No automatic underscore ivar in Xcode 5.1.1


I just noticed that, for some reason, I don't seem to have automatically created underscore iVars in my iOS 7 project, and I wonder why that is. My setup:

MyClass.h

@property (readonly) NSNumber *aNumber;

MyClass.m

@interface MyClass ()
@property (readwrite, strong) NSNumber *aNumber;
@end

@implementation MyClass

(...)

- (NSNumber *)aNumber {
    return _aNumber;
}

- (void)setANumber:(NSNumber *)aNumber {
    _aNumber = aNumber;
}

@end

This results in Use of undeclared identifier: '_aNumber'.

Why is that so? I thought that underscore iVars are always automatically synthesized? Is it because of the class extension I use? If I put in @synthesize aNumber = _aNumber; it (obviously) works.


Solution

  • There is one exception to the automatic synthesize rule.

    If you override both the getter and the setter of a property then you will have to manually synthesize the property.

    This has been the case ever since auto synthesis came in.

    Just add the @synthesize line and it will be fine.