编码/解码Swift的枚举(Xcode 6.1) [英] Encode/Decode enum for Swift (Xcode 6.1)
本文介绍了编码/解码Swift的枚举(Xcode 6.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有
var priority : Priority! = Priority.defaultPriority
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeInteger(priority.toRaw(), forKey: "priority") //toRaw may not yield the result I am expecting
}
required init(coder aDecoder: NSCoder) {
priority = aDecoder.decodeIntegerForKey("priority") //complaining about conflicting types
}
其枚举如下:
enum Priority : Int {
case defaultPriority = 0
case lowPriority = 1
case mediumPriority = 2
case highPriority = 3
}
这是编码/解码的最好方法是什么?
What is the best way to encode/decode this?
推荐答案
Priority.init(rawValue:)
应该可以工作。
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeInteger(priority.rawValue, forKey: "priority")
}
required init(coder aDecoder: NSCoder) {
priority = Priority(rawValue: aDecoder.decodeIntegerForKey("priority"))
}
这篇关于编码/解码Swift的枚举(Xcode 6.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文