在我的应用程序中,登录后获取的用户数据保存在首选项中,如下所示。 xcode 显示 NSKeyedUnarchiver.unarchiveObject(with:) 和 NSKeyedArchiver.archivedData(withRootObject:) 已从 iOS 12 弃用的警告。但应用程序在最新的 iOS 版本之前运行良好。为了使其稳定,如何将其更改为推荐的 unarchivedObjectOfClass:fromData:error:
static var userLoginResponse : LoginModel? {
get {
if let data = UserDefaults.standard.data(forKey: "userLoginResponse"), let respData = NSKeyedUnarchiver.unarchiveObject(with: data) as? Data {
return LoginModel(data: respData)
}
return nil
}
set {
if let value = newValue?.toJsonData() {
UserDefaults.standard.setValue(NSKeyedArchiver.archivedData(withRootObject: value), forKey: "userLoginResponse")
} else {
UserDefaults.standard.setValue(nil, forKey: "userLoginResponse")
}
}
}
根据苹果文档,unarchivedObjectOfClass:fromData:error: 将取消存档保存为 NSCoded 对象的数据。如何在不影响现有应用程序用户的情况下更改此现有实施?
将函数替换为:
try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSData.self, from: data) as? Data