This is my generic class:
open class SMState<T: Hashable>: NSObject, NSCoding {
open var value: T
open var didEnter: ( (_ state: SMState<T>) -> Void)?
open var didExit: ( (_ state: SMState<T>) -> Void)?
public init(_ value: T) {
self.value = value
}
convenience required public init(coder decoder: NSCoder) {
let value = decoder.decodeObject(forKey: "value") as! T
self.init(value)
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(value, forKey: "value")
}
}
Then I want to do this:
let stateEncodeData = NSKeyedArchiver.archivedData(withRootObject: currentState)
UserDefaults.standard.set(stateEncodeData, forKey: "state")
In my case currentState
is of type SMState<SomeEnum>.
But when I call NSKeyedArchiver.archivedData
, Xcode (9 beta 5) shows a message in purple saying:
Attempting to archive generic Swift class 'StepUp.SMState<StepUp.RoutineViewController.RoutineState>' with mangled runtime name '_TtGC6StepUp7SMStateOCS_21RoutineViewController12RoutineState_'. Runtime names for generic classes are unstable and may change in the future, leading to non-decodable data.
I am not exactly sure what it tries to say. Is not possible to save a generic object ?
Is there any other way to save a generic custom object ?
edit
:
Even if I use AnyHashable
instead of generics I get the same error on runtime when calling NSKeyedArchiver.archivedData
:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: : unrecognized selector sent to instance