swift3 - 在 userdefaults 中存储服务器响应时....我收到错误
问题描述
在 userdefaults 中存储服务器响应时......我收到错误......
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_SwiftValue encodeWithCoder:]:无法识别的选择器发送到实例 0x600002fe7720”
这是我的代码
if let responseObject = response.array {
print(responseObject)
let bookingDetails = responseObject[0].dictionary!
let myData = NSKeyedArchiver.archivedData(withRootObject:bookingDetails)
UserDefaults.standard.set(myData, forKey: "userJson")
}
解决方案
为了能够归档自定义类,该类必须采用NSCoding
. 这就是错误消息告诉您的内容。
JSON (in SwiftyJSON
) 是一个结构,它根本无法存档NSKeyedArchiver
。
删除SwiftyJSON
and NSCoding
,Codable
改为使用。
推荐阅读
- xamarin - 如何创建充当元素包装器的 XAML 模板?
- bash - Git 预提交钩子检查字符串,如果字符串存在则退出代码 1
- android - 单个 android 应用程序中具有多用户数据库的房间
- java - ScheduledExecutorService 在本地和测试服务器中工作,但在实时服务器中只运行一次
- javascript - 如何使用本机 javascript 合并数组内的对象
- javascript - React 中的自定义单词翻译器
- mysql - Amazon Web Service RDS 连接失败
- javascript - 仅使用 jquery ajax 保存在文件系统图像中
- algorithm - Implementation: Algorithm for a special distribution Problem
- python - 带有值和字典的熊猫元组进入数据框