编码和解码自定义子类对象会导致致命错误:在展开Optional值时意外发现nil

时间:2017-09-16 20:07:59

标签: swift nsuserdefaults nscoding nskeyedarchiver nskeyedunarchiver

我有2个对象,1是另一个的子类。当我尝试取消归档子对象时,即使我手动测试归档和取消归档对象,我也会获得fatal error: unexpectedly found nil while unwrapping an Optional value

具体来说,我崩溃了:

let subobjectsArray = NSKeyedUnarchiver.unarchiveObject(with: subobjects) as? [Subobject] {

调用Subobject

的解码初始值设定项
required init(coder aDecoder: NSCoder) {
            degree1 = aDecoder.decodeObject(forKey: degree1Key) as! CLLocationDegrees // get nil error here

这使我得出结论我必须错误地编码和解码子对象,但我不知道我在编码方面做错了什么。

如果有人能让我知道我做错了什么,我将不胜感激!

代码

测试存档和取消存档的功能:

func unarchiveSubobjects() -> [Subobject] {
    //Manually encode a subobject
    let subobjects = [Subobject(name: "testing", degree1: CLLocationDegrees(45))]
    let data = NSKeyedArchiver.archivedData(withRootObject: subobjects)
    userDefaults.set(data, forKey: Key.UserDefaults.subobjects)

    // Decode object does not work and crashes with nil error
    if let subobjects = userDefaults.value(forKey: Key.UserDefaults.subobjects) as? Data,
        let subobjectsArray = NSKeyedUnarchiver.unarchiveObject(with: subobjects) as? [Subobject] { // crashes on the 2nd let statement
        return subobjectsArray
    }
    return [Subobject]()
}

Subobject.swift

class Subobject: Superobject {
    var degree1: CLLocationDegrees

    private let degree1Key = "degree1"

    init(name: String, degree1: CLLocationDegrees) {
        self.degree1 = degree1

        super.init(name: name)
    }

    // MARK: NSCoding

    required init(coder aDecoder: NSCoder) {
        degree1 = aDecoder.decodeObject(forKey: degree1Key) as! CLLocationDegrees // get nil error here

        super.init(coder: aDecoder)
    }

    public override func encode(with aCoder: NSCoder) {        
        aCoder.encode(self.degree1, forKey: degree1Key)

        super.encode(with: aCoder)
    }
}

Superobject.swift

class Superobject: NSObject, NSCoding {

    var name: String

    private let nameKey = "name"

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

    // MARK: NSCoding

    required init(coder aDecoder: NSCoder) {
        name = aDecoder.decodeObject(forKey: nameKey) as! String
    }

    public func encode(with aCoder: NSCoder) {
        aCoder.encode(self.name, forKey: nameKey)
    }
}

1 个答案:

答案 0 :(得分:1)

不是解码对象而是使用

解码实际类型Double
degree1 = aDecoder.decodeDouble(forKey: degree1Key)!

由于CLLocationDegreesDouble的类型别名,它应该可以工作,或者您需要桥接转换值。