I have the following class that conforms to NSObject
and NSCoding
:
class PredicPair: NSObject, NSCoding {
var weight : Float
var prediction : String
init(weight: Float, prediction: String) {
self.weight = weight
self.prediction = prediction
}
required init(coder aDecoder: NSCoder) {
weight = aDecoder.decodeObject(forKey: "weight") as! Float
prediction = aDecoder.decodeObject(forKey: "prediction") as! String
}
func encode(with aCoder: NSCoder) {
aCoder.encode(weight, forKey: "weight")
aCoder.encode(prediction, forKey: "prediction")
}
}
In another class, I create the following variable words
of type [String:[PredicPair]]
which I then try to save using UserDefaults
with the following: UserDefaults.standard.set(self.words, forKey: self.WordStoreKey())
. Upon runtime, I'm met with the following exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object
Why is that happening? Aren't I already conforming to NSCoding?