How to change the deprecated function NSKeyedUnarchiver.unarchiveObject(with:) to unarchivedObjectOfClass:fromData:error:

huangapple go评论66阅读模式
英文:

How to change the deprecated function NSKeyedUnarchiver.unarchiveObject(with:) to unarchivedObjectOfClass:fromData:error:

问题

在我的应用程序中,登录后获取的用户数据保存在偏好设置中,如下所示。Xcode显示警告,指出从iOS 12开始,NSKeyedUnarchiver.unarchiveObject(with:) 和 NSKeyedArchiver.archivedData(withRootObject:) 已弃用。但是应用程序在最新的iOS版本上仍然正常运行。为了使其更稳定,我应该如何将其更改为建议使用的 unarchivedObjectOfClass:fromData:error: 方法?

static var userLoginResponse: LoginModel? {
    get {
        if let data = UserDefaults.standard.data(forKey: "userLoginResponse") {
            do {
                return try NSKeyedUnarchiver.unarchivedObject(ofClasses: [LoginModel.self], from: data)
            } catch {
                print("Failed to unarchive userLoginResponse: \(error)")
                return nil
            }
        }
        return nil
    }
    set {
        if let value = newValue?.toJsonData() {
            do {
                let archivedData = try NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: false)
                UserDefaults.standard.set(archivedData, forKey: "userLoginResponse")
            } catch {
                print("Failed to archive userLoginResponse: \(error)")
            }
        } else {
            UserDefaults.standard.set(nil, forKey: "userLoginResponse")
        }
    }
}

根据苹果文档的建议,使用 unarchivedObjectOfClass:fromData:error: 方法可以反序列化保存为 NSCoded 对象的数据。如何更改现有实现,而不影响现有的应用程序用户?

英文:

In my application, the user data getting after login is saved in the preferences as below. The xcode shows the warning that NSKeyedUnarchiver.unarchiveObject(with:) and NSKeyedArchiver.archivedData(withRootObject:) are deprecated from iOS 12. But application is working fine till the latest iOS versions. To make it stable, how can I change it to unarchivedObjectOfClass:fromData:error: which is recommended.

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")
            }
        }
    }

As per the apple documentation, unarchivedObjectOfClass:fromData:error: will unarchive the data saved as NSCoded object. How can I change this existing implementation without affect the existing app users ?

答案1

得分: 1

尝试将函数替换为:

try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSData.self, from: data) as? Data
英文:

Replace the function by:

try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSData.self, from: data) as? Data

答案2

得分: 1

你打算归档已经符合 PropertyList 标准的 Data。移除 NSKeyed(Un)archiver 部分。

static var userLoginResponse: LoginModel? {
    get {
        guard let data = UserDefaults.standard.data(forKey: "userLoginResponse") else { return nil }
        return LoginModel(data: data)
    }
    set {
        UserDefaults.standard.set(newValue?.toJsonData(), forKey: "userLoginResponse")
    }
}

附注:除非你是指使用 KVC(你不是),否则永远不要使用 setValue(_:forKey:)

英文:

You are going to archive Data which is already PropertyList compliant. Remove the NSKeyed(Un)archiver stuff.

static var userLoginResponse : LoginModel? {
    get {
        guard let data = UserDefaults.standard.data(forKey: "userLoginResponse") else { return nil }
        return LoginModel(data: data)
    }
    set {
        UserDefaults.standard.set(newValue?.toJsonData(), forKey: "userLoginResponse")
    }
}

Side-note: Never use setValue(_:forKey:) unless you mean KVC (you don't).

huangapple
  • 本文由 发表于 2023年3月9日 16:40:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定