I have some subclass of NSObject
that defines a string representable Enum
within in at follows:
class SomeClass: NSObject {
enum Direction: String {
case north = "North"
case south = "South"
}
public var direction: Direction
init(direction: Direction) {
self.direction = direction
super.init()
}
}
I want to allow this class to be encoded and decoded, so I use the following:
func encode(with aCoder: NSCoder) {
aCoder.encode(direction, forKey: #keyPath(direction))
}
public convenience required init?(coder aDecoder: NSCoder) {
self.init(direction: aDecoder.decodeObject(forKey: #keyPath(direction)) as! Direction)
}
However, I get the following error Argument of '#keyPath' refers to non-'@objc' property 'direction' and when I change the definition of direction
to
@objc public var direction: Direction
I get the error Property cannot be marked @objc because its type cannot be represented in Objective-C.
What's the best way to avoid this error, please?
Thanks for any help.
EDIT
The comments explain the problem. This answers in this link suggest how to accomplish using a String
raw representable.