wqyfavor wqyfavor - 5 months ago 62
Swift Question

Fail to decode Int with NSCoder in Swift

I am using Xcode8 Beta with Swift 3.0. I tried to encode a simple object base on NSObject, but I cannot decode Int or NSInteger type. (The encoding process is OK)

enter image description here

codes

class Model : NSObject, NSCoding {
var seq: NSNumber?
var seq2: Int? // problem with seq2, NSInteger is not ok, either
var id: String?
var value: String?

override init() {
super.init()
}

required init?(coder aDecoder: NSCoder){
self.seq = aDecoder.decodeObject(forKey: "seq") as? NSNumber
self.seq2 = aDecoder.decodeInteger(forKey: "seq2")
self.id = aDecoder.decodeObject(forKey: "id") as? String
self.value = aDecoder.decodeObject(forKey: "value") as? String
}

func encode(with aCoder: NSCoder){
aCoder.encode(seq, forKey: "seq")
aCoder.encode(seq2, forKey: "seq2")
aCoder.encode(id, forKey: "id")
aCoder.encode(value, forKey: "value")
}
}

Rob Rob
Answer

The problem is that seq2 is not an Int, but rather a Int? optional. It cannot be represented as an Objective-C integer.

You can use decodeObject:

required init?(coder aDecoder: NSCoder){
    self.seq = aDecoder.decodeObject(forKey: "seq") as? NSNumber
    self.seq2 = aDecoder.decodeObject(forKey: "seq2") as? Int
    self.id = aDecoder.decodeObject(forKey: "id") as? String
    self.value = aDecoder.decodeObject(forKey: "value") as? String

    super.init()
}

or change it so it is not optional:

class Model : NSObject, NSCoding {
    var seq: NSNumber?
    var seq2: Int
    var id: String?
    var value: String?

    init(seq: NSNumber, seq2: Int, id: String, value: String) {
        self.seq = seq
        self.seq2 = seq2
        self.id = id
        self.value = value

        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        self.seq = aDecoder.decodeObject(forKey: "seq") as? NSNumber
        self.seq2 = aDecoder.decodeInteger(forKey: "seq2")
        self.id = aDecoder.decodeObject(forKey: "id") as? String
        self.value = aDecoder.decodeObject(forKey: "value") as? String

        super.init()
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(seq, forKey: "seq")
        aCoder.encode(seq2, forKey: "seq2")
        aCoder.encode(id, forKey: "id")
        aCoder.encode(value, forKey: "value")
    }

    override var description: String { return "<Model; seq=\(seq); seq2=\(seq2); id=\(id); value=\(value)>" }
}
Comments