1

In my view controller's header file I've created a property:

@property(nonatomic) BOOL hasPhoto;

In my implementation file, I want to override its setter, hence:

-(void)setHasPhoto:(BOOL)hasPhoto{
    _hasPhoto = hasPhoto;
    if(hasPhoto){
        //do something
    }
}

However, when I try to compile, Xcode doesn't see the backing variable, which should be named _hasPhoto and doesn't compile. If I synthesize it manually:

@synthesize hasPhoto = _hasPhoto;

It works. What am I doing wrong?

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

1 Answers1

0

Clang (the compiler behind Xcode) will only generate the backing ivar for a property if the property is declared in the interface for a class - otherwise you need to synthesize it or implement the methods for the property yourself. It will not generate the ivar for you if the property is defined in a protocol or category on the class. My guess is that you have declared the property in a protocol, in which case you will need to use @synthesize hasPhoto=_hasPhoto.

// This is just a forward declaration of the protocol
@protocol MyProto;

@interface ZZZMyController : UIViewController<MyProto>

// This property will get an ivar automatically generated
@property (nonatomic) BOOL hasPhoto;

@end

@interface ZZZMyController(ExtraStuff)

// This property will NOT get an ivar automatically, you would need to synthesize it explicitly or create your own implementations of both methods
@property (nonatomic) BOOL categoryBool;

@end

@protocol MyProto <NSObject>

// This property will NOT get an ivar automatically either, you would need to synthesize it explicitly or create your own implementations of both methods
@property (nonatomic) protoBool;

@end

If this answer doesn't fix your problem, I would recommend removing and reinstalling Xcode. If that doesn't work, file a bug report.

Community
  • 1
  • 1
RyanR
  • 7,728
  • 1
  • 25
  • 39
  • Unfortunatelly, that's also not the case. The property is defined in a regular class interface in a header file with many other properties, with no problem. it is not in an M file, not in a protocol declaration, nor in a extension/category. I don't think reinstalling Xcode will change much (I've already purged the derived data and cleaned build folder, restarted Xcode etc.), I think I'll file a bug report. – Can Poyrazoğlu Sep 01 '14 at 20:09