简体   繁体   English

Objective-C 枚举符合 RawRepresentable

[英]Objective-C enums conform to RawRepresentable

Any exprerience on this I have for example the following enum written in objc对此我有任何经验,例如以下用 objc 编写的枚举

typedef enum {
  Type1,
  Type2
} Type;

extension Type: RawRepresentable {
    typealias RawValue = UInt32
}

compiler crashes when I'm trying to conform to RawRepresentable.The only thing that I can imaging is that RawRepresentable works only with swift enums.当我试图符合 RawRepresentable 时,编译器崩溃。我唯一能想象的是 RawRepresentable 只适用于 swift 枚举。

Any ideas?有任何想法吗?

Forget about using raw C enum and use the Objective-C NS_ENUM macro:忘记使用原始 C 枚举并使用 Objective-C NS_ENUM宏:

typedef NS_ENUM(NSInteger, MyEnumType) {
    Type1,
    Type2
};

Then in Swift the enum will be already RawRepresentable .然后在 Swift 中,枚举将已经是RawRepresentable You cannot add that conformance this way.您不能以这种方式添加该一致性。 Well, you probably could, but you will have to also declare init?(rawValue:) and var rawValue .好吧,您可能可以,但是您还必须声明init?(rawValue:)var rawValue

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

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