Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

9个月前关闭。



Improve this question




尝试从此处实现KeychainWrapper:https://github.com/jrendel/SwiftKeychainWrapper
它运行良好,但是在一段代码中我犯了一个错误:“'unarchiveObject(with :)'在iOS 12.0中已被弃用:使用+ unarchivedObjectOfClass:fromData:error:代替”

我试图遵循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

最佳答案

您可以按照以下方式执行此操作...

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

关于ios - 如何处理不赞成使用的功能'unarchiveObject(with :)'? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59525944/

10-10 09:26