swift - 通过解码器查看还有哪些数据需要解码
问题描述
我正在使用 Codable 的 Swiftt 4 项目中工作。但是,我收到以下错误:
No value associated with key CodingKeys(stringValue: "postable_type", intValue: nil) ("postable_type").
现在我想知道如何最好地调试它。是否有可能查看 Codable 试图在我的模型中塞入哪些数据?特别是因为在这种情况下使用了相当高级的嵌套,我认为它在这里会是一个真正的救星......
谢谢 :)
解决方案
Codable
错误非常具有描述性。对于调试,请使用此catch
块获取详细的错误消息:
} catch DecodingError.dataCorrupted(let context) {
print(context)
} catch DecodingError.keyNotFound(let key, let context) {
print("Key '\(key)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch DecodingError.valueNotFound(let value, let context) {
print("Value '\(value)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch DecodingError.typeMismatch(let type, let context) {
print("Type '\(type)' mismatch:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch {
print("error: ", error)
}
两者都debugDescription
告诉codingPath
你到底哪里出了问题。
推荐阅读
- css - vs code live sass 编译器不更新文件
- javascript - 提交突变后 QueryRenderer 不更新
- qt - 如何检测 qt 标签(或任何小部件)是否具有所需的所有空间?
- xaml - Xamarin.Forms:如何自动隐藏第一个 CollectionView
- java - 如何在scala中的for循环中应用继续并产生多个值?
- c++ - 将向量移动到 unordered_set
- r - 用插值替换缺失数据
- javascript - 我如何将日期从 appcomponent .ts 传递到对话框?
- c# - 如何从在多台笔记本电脑上运行的 WPF 应用程序访问白名单 Azure SQL db
- php - 如何在小部件后端创建表单并将其显示在小部件末尾的某个区域?