如何处理已弃用的函数'unarchiveObject(with:)'? [关闭]
Posted
技术标签:
【中文标题】如何处理已弃用的函数\'unarchiveObject(with:)\'? [关闭]【英文标题】:How to deal with deprecated function 'unarchiveObject(with:)'? [closed]如何处理已弃用的函数'unarchiveObject(with:)'? [关闭] 【发布时间】:2020-04-18 21:52:02 【问题描述】:尝试从这里实现 KeychainWrapper:https://github.com/jrendel/SwiftKeychainWrapper 它运行良好,但在一段代码中我出错了:“'unarchiveObject(with:)' was deprecated in ios 12.0: Use +unarchivedObjectOfClass:fromData:error: instead”
我尝试关注discussion,看起来很相似,但没有成功。
这段代码在这里:
open func object(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> NSCoding?
guard let keychainData = data(forKey: key, withAccessibility: accessibility) else
return nil
return NSKeyedUnarchiver.unarchiveObject(with: keychainData) as? NSCoding
How to NSKeyedUnarchiver.unarchiveObject
这是更新版本:
open func object(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> NSCoding?
guard let keychainData = data(forKey: key, withAccessibility: accessibility) else
return nil
let result = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(keychainData) as? NSCoding
return result
【问题讨论】:
请参考***.com/a/59170370/6920041 【参考方案1】:您可以通过以下方式执行此操作...
let result = jsonDict["result"] as? NSDictionary ?? [:]
let data = try! NSKeyedArchiver.archivedData(withRootObject: result, requiringSecureCoding: false)
UserDefaults.standard.set(data, forKey: "currentUser")
// 从 Userdefault 获取数据
let result = UserDefaults.standard.data(forKey: "currentUser")
if result != nil
let dict = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(result!) as? NSDictionary ?? [:]
print("Current User Details : - \(dict)")
【讨论】:
【参考方案2】:感谢Himanshu Patel 的提示,它对我有用。这是更新的代码:
open func object(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> NSCoding?
guard let keychainData = data(forKey: key, withAccessibility: accessibility) else
return nil
let result = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(keychainData) as? NSCoding
return result
【讨论】:
欢迎兄弟,请点赞我的回答。 Himanshu Patel 我无法支持您的评论。请写一个简短的答案,我很乐意投票。 这是我的回答***.com/a/59170370/6920041 是的,但您不是由我发布的 :) 我只能对我的问题的答案进行投票。 :)以上是关于如何处理已弃用的函数'unarchiveObject(with:)'? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章