gpt4 book ai didi

swift - NSKeyedArchiver:转换数据返回 nil - Swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:54:20 25 4
gpt4 key购买 nike

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

@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
}
}

unarchived 返回 nil,但键“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 或 Property List。它摆脱了 @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")
}
}

关于swift - NSKeyedArchiver:转换数据返回 nil - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54656275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com