0

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?

cyril
  • 3,020
  • 6
  • 36
  • 61
  • Implementing NSCoding is not sufficient, you have to *archive* the instances to `NSData` objects, which can be stored in the user defaults. – Martin R Aug 24 '16 at 18:50
  • @MartinR I've also tried what's listed in the linked question and although the compiler no longer throws an error, the retrieved dictionary is empty. Any reason why? – cyril Aug 24 '16 at 20:34

0 Answers0