【发布时间】:2021-12-26 02:36:18
【问题描述】:
我正在 swift 中构建一个应用程序,它显示多个图表,其中一个是汇率图表,可以通过 UITableView 中的货币代码和日期范围进行过滤。我能够成功地从https://fixer.io/ 中提取 FX 数据并将该 json 数据转换为以下 swift 结构:
struct FetchedFXRateByDate: Codable {
let success, timeseries: Bool
let startDate, endDate, base: String
let rates: [String: [String: Double]] // [Date: [Currency Code : Amount]]
enum CodingKeys: String, CodingKey {
case success, timeseries
case startDate = "start_date"
case endDate = "end_date"
case base, rates
}
}
我现在想要的是操作或“映射”/“过滤”内部 dict 'let rates: [String: [String: Double]]',以转换 dict:
发件人:
[String: [String: Double]] // [日期: [货币代码 : Amount]]
收件人:
[String: [String: Double]] // [货币代码: [Date : Amount]]
有效地交换密钥。这可以通过带有键的 for 循环轻松完成,但我需要一种更有效的方法来完成任务。这样我就可以在下面的界面中绘制图形:
非常感谢任何帮助!
【问题讨论】:
标签: swift dictionary filter nested mapping