首页 > 解决方案 > 在 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")

}

标签: swift3

解决方案


为了能够归档自定义类,该类必须采用NSCoding. 这就是错误消息告诉您的内容。

JSON (in SwiftyJSON) 是一个结构,它根本无法存档NSKeyedArchiver

删除SwiftyJSONand NSCodingCodable改为使用。


推荐阅读