5

I have this simple class

import UIKit

class SimpleModel: NSObject, NSCoding {

    var name : String!
    var done : Bool!

    init(name:String) {
        self.name = name
        self.done = false
    }

    internal required init?(coder aDecoder: NSCoder) {
        self.name = aDecoder.decodeObject(forKey: "name") as! String
        self.done = aDecoder.decodeBool(forKey: "done") // BUG HERE
    }

    func encode(with encoder: NSCoder) {
        encoder.encode(self.name, forKey: "name")
        encoder.encode(self.done, forKey: "done")
    }
}

the save code:

let data = NSKeyedArchiver.archivedData(withRootObject: storageArray)
UserDefaults.standard.set(data, forKey: "storage")
UserDefaults.standard.synchronize()

the read code:

if let data = UserDefaults.standard.data(forKey: "storage") {
    storageArray = NSKeyedUnarchiver.unarchiveObject(with: data) as! [SimpleModel]
}

the problem occurs when the NSKeyedUnarchiver does it's job. I can not understand where the problem comes from.

Thanks!

Enlil
  • 1,027
  • 14
  • 28

3 Answers3

14

the trick is remove ! form the primitive types. If you put ! you are saying "make an implicit-unwrapped optional" so the encoder will archive as NSNumber instead of Bool (or Int, Double). If you remove ! the encoder will archive as Bool and things works as expected (I spent an "incident" and this solution is provided by Apple)

Enlil
  • 1,027
  • 14
  • 28
  • I could not understand above, I got the solution from this answer: https://stackoverflow.com/a/39656687/1298362 – g212gs Sep 15 '17 at 13:33
12

Bool and Int have new methods:

 self.x = Int(decoder.decodeCInt(forKey: "Speed"))
 self.y = decoder.decodeObject(forKey: "Serial") as! String
 self.z = Bool(decoder.decodeBool(forKey: "Direction") )
Eiko
  • 25,601
  • 15
  • 56
  • 71
Sameer C
  • 129
  • 2
  • Last line can be written simply as: self.z = decoder.decodeBool(forKey: "KeyName"), because decodeBool already returns Bool – ppalancica Oct 27 '18 at 12:15
8

I had the same Problem. Try this:

self.done = aDecoder.decodeObject(forKey: "done") as? Bool ?? aDecoder.decodeBool(forKey: "done")
mswyss
  • 322
  • 3
  • 12