简体   繁体   English

NS_ENUM作为协议中的属性

[英]NS_ENUM as property in protocol

I came across to strange behaviour. 我遇到了奇怪的行为。 I used to have: 我曾经有过:

@property (nonatomic) ApplicationState applicationState;

directly in my Application class. 直接在我的Application类中。 Now it's extracted to protocol 现在将其提取到协议

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

ApplicationState is Enum ApplicationState是枚举

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

Now. 现在。 It used to work. 它曾经工作。 Now such line: self.applicationState = ApplicationStateMaximized; 现在这样的行: self.applicationState = ApplicationStateMaximized; called from implementing class causes no effect. 从实现类调用不会产生任何效果。


UPDATE 更新

Agy , rickster you're both right. AGYrickster你都是对的。 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) 我没有注意到的是,我的同事添加了getter,该方法始终返回相同的值(不幸的是,IDE在我重复类中的属性之前不会显示此访问器)

Declaring a @property in a protocol doesn't synthesize storage or accessors for that property in classes that adopt the protocol. 在协议中声明@property不会在采用该协议的类中为该属性合成存储或访问器。 For that, you'll want something like this: 为此,您需要这样的东西:

@implementation Application
@synthesize applicationState = _applicationState;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM