我有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)
}
}
答案 0 :(得分:1)
不是解码对象而是使用
解码实际类型Double
degree1 = aDecoder.decodeDouble(forKey: degree1Key)!
由于CLLocationDegrees
是Double
的类型别名,它应该可以工作,或者您需要桥接转换值。