NS_ENUM as property in protocol

98 Views Asked by At

I came across to strange behaviour. I used to have:

@property (nonatomic) ApplicationState applicationState;

directly in my Application class. Now it's extracted to protocol

@protocol ApplicationProtocol <NSObject>
@property (nonatomic) ApplicationState applicationState;

ApplicationState is Enum

typedef NS_ENUM(NSUInteger, ApplicationState)
{
    ApplicationStateNormal = 0,
    ApplicationStateExpanded = 1, 
    ApplicationStateMaximized = 2
};

Now. It used to work. Now such line: self.applicationState = ApplicationStateMaximized; called from implementing class causes no effect.


UPDATE

Agy, rickster you're both right. I forgot add to this question note, that I have already synthesized properties in implementing class. What I haven't notice, that my colleague added getter which returned always the same value (unfortunatelly IDE deosn't show this accessor until I duplicated property in my class)

2

There are 2 best solutions below

0
On BEST ANSWER

You needs to synthesize the property:

@implementation Application

@synthesize applicationState = _ applicationState;

@end

or declare the property again:

@interface Application : NSObject <ApplicationProtocol>

@property (nonatomic) ApplicationState applicationState;

@end
0
On

Declaring a @property in a protocol doesn't synthesize storage or accessors for that property in classes that adopt the protocol. For that, you'll want something like this:

@implementation Application
@synthesize applicationState = _applicationState;