NSKeyedArchiver:投射数据返回零 - Swift

NSKeyedArchiver: casting Data returning nil - Swift

好吧,在这里调查了几个类似的主题,并按照建议完成了所有操作,但是在尝试将解码对象转换为我的类型时,我计算出的 属性 "previousUserData" returns 为零。怎么了?

@objc class PreviousUserData: NSObject, NSCoding {

var name: String
var phone: String
var email: String

func encode(with aCoder: NSCoder) {
    aCoder.encode(name, forKey: "name")
    aCoder.encode(phone, forKey: "phone")
    aCoder.encode(email, forKey: "email")
}

required convenience init?(coder aDecoder: NSCoder) {
    guard
        let name = aDecoder.decodeObject(forKey: "name") as? String,
        let phone = aDecoder.decodeObject(forKey: "phone") as? String,
        let email = aDecoder.decodeObject(forKey: "email") as? String
        else {
            return nil
    }
    self.init(name: name, phone: phone, email: email)
}

init(name: String, phone: String, email: String) {
    self.name = name
    self.phone = phone
    self.email = email
}
}

未存档 returns 我没有,但是键 "userdata" 的数据存在

    var previousUserData: PreviousUserData? {
    get {
        if let object = UserDefaults.standard.object(forKey: "userdata") as? Data {
            let unarchived = NSKeyedUnarchiver.unarchiveObject(with: object) as? PreviousUserData
            return unarchived
        }
        return nil
    }
    set {
        let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: previousUserData as Any)
        UserDefaults.standard.setValue(encodedData, forKey: "userdata")
    }
    }

实际上您无法获得有效数据,因为 setter 是错误的。您必须保存 newValue 而不是 previousUserData

这是一个稍微优化的版本

var previousUserData: PreviousUserData? {
    get {
        guard let data = UserDefaults.standard.data(forKey: "userdata") else { return nil }
        return NSKeyedUnarchiver.unarchiveObject(with: data) as? PreviousUserData
    }
    set {
        guard let newValue = newValue else { return }
        let encodedData = NSKeyedArchiver.archivedData(withRootObject: newValue)
        UserDefaults.standard.set(encodedData, forKey: "userdata")
    }
}

NSCoding 很重。在这种情况下,我建议使用 Codable 将数据序列化为 JSON 或 属性 列表。它摆脱了 @objcclassNSObject 并将整个代码减少为

struct PreviousUserData : Codable {
    var name: String
    var phone: String
    var email: String
}

var previousUserData: PreviousUserData? {
    get {
        guard let data = UserDefaults.standard.data(forKey: "userdata") else { return nil }
        return try? JSONDecoder().decode(PreviousUserData.self, from: data)
    }
    set {
        guard let newValue = newValue, let encodedData = try? JSONEncoder().encode(newValue) else { return }
        UserDefaults.standard.set(encodedData, forKey: "userdata")
    }
}