The code was above, but here it is. My confusion is how to handle the constant, conversionRates. I know things like the constant, result. But for conversion rates… How would I be able to write the string portion and hide the double, for instance… Together wouldn’t I just re-declare it into a string so it’d all work?
struct Currency: Codable {
let result: String
let conversionRates: [String: Double]
enum CodingKeys: String, CodingKey {
case result
case conversionRates = "conversion_rates"
}
}
My fetch request
// Beginning to get currency
func fetchStocks() {
let urlString = "https://v6.exchangerate-api.com/v6/3af64b8ec60e3e731a5c3068/latest/USD"
let url = URL(string: urlString)
guard url != nil else {
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: url!) { data, response, error in
// Check for errors
if error == nil && data != nil {
do {
// Parse JSON
let decoder = JSONDecoder()
let currency = try decoder.decode(Currency.self, from: data!)
print(currency)
}
catch {
print("Error in json parsing")
}
}
}
// Make the API Call
dataTask.resume()
}// End of Stocks API