I'm building an app in swift which displays several graphs, one of which is a graph of exchange rates which can be filtered by currency code and date range within a UITableView. I'm able to successfully pull the FX data from https://fixer.io/ and convert that json data into the following swift struct:
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
}
}
What I now want, is to manipulate or 'map'/'filter' the inner dict 'let rates: [String: [String: Double]]', to convert the dict:
From:
[String: [String: Double]] // [Date: [Currency Code : Amount]]
To:
[String: [String: Double]] // [Currency Code: [Date : Amount]]
Effectively swapping the keys. This can easily be done by a for loop with the keys, but I need a more efficient way to achieve the task. This is so i can draw the graph in the below interface:
Any help is greatly appreciated!