I have
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
}
with the enum being the following:
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:)
should work.
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeInteger(priority.rawValue, forKey: "priority")
}
required init(coder aDecoder: NSCoder) {
priority = Priority(rawValue: aDecoder.decodeIntegerForKey("priority"))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With