swift - 解码/编码以日期为键的字典
问题描述
尝试对具有Date
as 键的字典进行编码/解码会产生一些令人惊讶的结果。
以下代码说明了该行为:
let data = """
{ "20210507": "Person1", "20210508": "Person2" }
""".data(using: .utf8)!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(dateFormatter)
try print(decoder.decode([Date: String].self, from: data))
天真地,有人会认为解码会成功,但事实并非如此,我们会抛出以下错误:
Fatal error: Error raised at top level: Swift.DecodingError.typeMismatch(Swift.Array<Any>,
Swift.DecodingError.Context(codingPath: [],
debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
所以解码器被告知解码字典,它以某种方式期望一个数组。
转到编码方面:
let dict: [Date: String] = [Date(): "Person1",
Date().addingTimeInterval(86400): "Person2"]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .formatted(dateFormatter)
let data = try encoder.encode(dict)
print(String(data: data, encoding: .utf8)!)
这有效,但是它输出:
["20211007","Person1","20211008","Person2"]
,它解释了在尝试解码字典(又名 JSON 对象)之前收到的错误。
为了确保行为,我将解码数据替换为
["20210507", "Person1", "20210508", "Person2"]
,并且解码成功,第一个片段的解码代码没有其他更改。
因此,似乎在将日期作为字典键处理时,解码器需要键+值对的数组,而不是字典。
有没有办法让它与标准Codable
基础设施一起工作?
另一种方法是 decode [String: String]
,然后解析日期,想知道是否有更清洁、更“Codabe-ish”的替代方案。
解决方案
推荐阅读
- blazor - Blazor 托管的调试器问题
- python - TypeError:无法腌制“_thread.RLock”对象并发期货
- node.js - Express 或 Axios 错误:套接字挂起代码:ECONNRESET
- swift - MKMapView 上的自定义标注
- ios - 如何在包含 AudioKit 的应用程序中最好地将滑音添加到当前正在播放的音符中?
- c# - 为什么 Task.Run 不处理 Task 上的所有行?
- arkit - 将节点放在相机前面并旋转
- javascript - Javascript:我可以只查询某个类之后的下 n 个元素吗?
- android - 应用样式后,SwitchMaterial 不会更改 textStyle
- sql - 如何创建将更新表的存储过程