ios - 字典没有在 swift 5 中保存在 Userdefault 中

标签 ios swift

我有以下测试代码

 if let responseJSON = try JSONSerialization.jsonObject(with: data!) as? NSDictionary{
 print(responseJSON)

 UserDefaults.standard.setValue(responseJSON, forKey: "user_auth_token")
//UserDefaults.standard.set(responseJSON, forKey: "user_auth_token")
print("\(UserDefaults.standard.value(forKey: "user_auth_token")!)")

}

但是在“UserDefaults.standard.setValue”这一行它给我错误

for key user_auth_token'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000113ab06fb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x0000000112ad3ac5 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000113ab0555 +[NSException raise:format:] + 197
    3   CoreFoundation                      0x00000001139cb82b _CFPrefsValidateValueForKey + 283
    4   CoreFoundation                      0x00000001139cbc95 -[CFPrefsSource setValues:forKeys:count:copyValues:removeValuesForKeys:count:from:] + 373
    5   CoreFoundation                      0x00000001139cbfbc -[CFPrefsSource setValues:forKeys:count:copyValues:from:] + 28
    6   CoreFoundation                      0x00000001139cc013 -[CFPrefsSource setValue:forKey:from:] + 67
    7   CoreFoundation                      0x0000000113aa759e __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 318
    8   CoreFoundation                      0x0000000113aa6e0a normalizeQuintuplet + 314
    9   CoreFoundation                      0x0000000113aa7454 -[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 100
    10  CoreFoundation                      0x0000000113a843fb -[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] + 91
    11  CoreFoundation                      0x0000000113a88065 _CFPreferencesSetAppValueWithContainer + 117
    12  Foundation                          0x00000001125bd563 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 55
)

这是什么意思?如何将我的数据存储到用户默认值中。 请帮忙。

最佳答案

您需要像这样从 userDefaults 存储和检索 JSON,只需要在保存自定义对象的同时归档数据。此外,如果您想将自定义类对象保存在用户默认值中,则需要在该类中实现 NSCoding/Codeable 协议(protocol)。

class func saveInUserDefauld(json: [String: Any]) {
    let myData = NSKeyedArchiver.archivedData(withRootObject: json)
    UserDefaults.standard.set(myData, forKey: "UserSession")
}

class func getUser()-> [String: Any]? {
    if let user = UserDefaults.standard.object(forKey: "UserSession")as? Data {
        let recovedUserJson = NSKeyedUnarchiver.unarchiveObject(with: user) as! [String: Any]
        return recovedUserJson
    } else {
        return nil
    }
}

关于ios - 字典没有在 swift 5 中保存在 Userdefault 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350970/

相关文章:

c++ - 如何将 Swift 与 C 或 C++ 连接?

swift - Xcode 10 Struct Initializer autoComplete 不显示

ios - 如何在 iOS 中使用核心图像准确检测图像中的人脸?

ios - 是否可以响应 UIAlertView 中的摇动手势?

ios - 带有 CocoaPods v0.38 的 ADAL iOS 使用 Swift 没有桥接头

ios - Swift 为 SKLabelNode 设置自定义字体颜色

Swift - AES 128 ctr,密文太长

ios-从后台状态恢复后保持特定事件 ViewController 的屏幕方向

ios - 如何在 uibezierpath 周围获得缓冲区

ios - cacheAsBitmap 对 AIR for iOS 中使用 scrollRect mask 的 Sprite 没有影响