我有以下类符合NSObject
和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")
}
}
在另一个课程中,我创建了words
类型的以下变量[String:[PredicPair]]
,然后我尝试使用UserDefaults
保存以下内容:UserDefaults.standard.set(self.words, forKey: self.WordStoreKey())
。在运行时,我遇到了以下异常:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object
为什么会这样? Aren我已经符合NSCoding了吗?