API 数据的初始化和存储错误
Initialization and Storage Error of API Data
我有一个汇率 API 初始化/存储问题。我读取了一些货币汇率,并希望将数据暂时存储在 moneyRates 中,然后将数据作为有序数据移动到 rateArray。我收到错误消息“初始化程序调用中没有完全匹配”。错误发生在以“let result = try JSONSerialization...”开头的行。我还在边栏中看到一条消息(Xcode 灰色!)“/Foundation.Data:29:23:候选人要求类型 'MoneyRates' 和 'UInt8' 等效(要求指定为 'S.Element' == 'UInt8')”。我猜我需要用某种格式信息来初始化 moneyRates。
我想要一些关于 moneyRates 错误的解释以及如何解决它。此时我不关心 rateArray。感谢您的帮助。
struct MoneyRates {
let date: String
let base: String
let rates: [String: Double]
}
class CurrencyRates: ObservableObject{
@Published var moneyRates: [MoneyRates]
@Published var rateArray = [Double] ()
init() {
if UserDefaults.standard.array(forKey: "rates") != nil {
rateArray = UserDefaults.standard.array(forKey: "rates") as! [Double]
} else {
rateArray = [Double] (repeating: 0.0, count: 170)
UserDefaults.standard.set(self.rateArray, forKey: "rates")
}
}
// retrieve exchange rates for all 150+ countries from internet and save to rateArray
func updateRates(baseCur: String) {
let baseUrl = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
let requestType = ".json"
guard let url = URL(string: baseUrl + baseCur + requestType) else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
do {
let result = try JSONSerialization.jsonObject(with: Data(moneyRates)) as! [String:Any] // <-- error is occurring here
var keys = Array(arrayLiteral: result.keys)
if let dateIndex = keys.firstIndex(of: "date"),
let date = result[keys[dateIndex]] as? String, keys.count == 2 {
keys.remove(at: dateIndex)
let base = keys.first!
let rates = MoneyRates(date: date, base: base, rates: result[base] as! [String:Double])
print(rates)
}
} catch {
print(error)
}
}
}.resume()
}
}
如果您尝试解码从 URLSession
获得的结果,那么您应该从 [=] 传递 data
而不是传递 Data(moneyRates)
进行解码14=] 闭包:
let result = try JSONSerialization.jsonObject(with: data) as! [String:Any]
我有一个汇率 API 初始化/存储问题。我读取了一些货币汇率,并希望将数据暂时存储在 moneyRates 中,然后将数据作为有序数据移动到 rateArray。我收到错误消息“初始化程序调用中没有完全匹配”。错误发生在以“let result = try JSONSerialization...”开头的行。我还在边栏中看到一条消息(Xcode 灰色!)“/Foundation.Data:29:23:候选人要求类型 'MoneyRates' 和 'UInt8' 等效(要求指定为 'S.Element' == 'UInt8')”。我猜我需要用某种格式信息来初始化 moneyRates。
我想要一些关于 moneyRates 错误的解释以及如何解决它。此时我不关心 rateArray。感谢您的帮助。
struct MoneyRates {
let date: String
let base: String
let rates: [String: Double]
}
class CurrencyRates: ObservableObject{
@Published var moneyRates: [MoneyRates]
@Published var rateArray = [Double] ()
init() {
if UserDefaults.standard.array(forKey: "rates") != nil {
rateArray = UserDefaults.standard.array(forKey: "rates") as! [Double]
} else {
rateArray = [Double] (repeating: 0.0, count: 170)
UserDefaults.standard.set(self.rateArray, forKey: "rates")
}
}
// retrieve exchange rates for all 150+ countries from internet and save to rateArray
func updateRates(baseCur: String) {
let baseUrl = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
let requestType = ".json"
guard let url = URL(string: baseUrl + baseCur + requestType) else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
do {
let result = try JSONSerialization.jsonObject(with: Data(moneyRates)) as! [String:Any] // <-- error is occurring here
var keys = Array(arrayLiteral: result.keys)
if let dateIndex = keys.firstIndex(of: "date"),
let date = result[keys[dateIndex]] as? String, keys.count == 2 {
keys.remove(at: dateIndex)
let base = keys.first!
let rates = MoneyRates(date: date, base: base, rates: result[base] as! [String:Double])
print(rates)
}
} catch {
print(error)
}
}
}.resume()
}
}
如果您尝试解码从 URLSession
获得的结果,那么您应该从 [=] 传递 data
而不是传递 Data(moneyRates)
进行解码14=] 闭包:
let result = try JSONSerialization.jsonObject(with: data) as! [String:Any]