Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我enum在我的 Swift 类中有一个并声明了一个变量。我需要使用NSCoder. 关于这句话我应该使用很多问题rawValue。Enum声明如下:
enum
NSCoder
rawValue
Enum
enum ConnectionType { case Digital, PWM }
但是在 Swift 1.2 中没有这样的初始化器。如何在 Swift 1.2 和 Xcode 6.3 中做到这一点?
您必须为枚举定义一个“原始类型”,例如
enum ConnectionType : Int { case Digital, PWM }
然后你可以用
aCoder.encodeInteger(type.rawValue, forKey: "type")
和解码
type = ConnectionType(rawValue: aDecoder.decodeIntegerForKey("type")) ?? .Digital
??如果解码的整数对枚举无效,则使用nil-coalescing 运算符提供默认值。
??