为什么我的自定义类在尝试将其保存在属性列表中时会崩溃?

时间:2016-08-24 18:47:21

标签: swift nscoding

我有以下类符合NSObjectNSCoding

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了吗?

0 个答案:

没有答案