我正在尝试使用适用于iOS的Alpha Vantage API构建显示货币汇率的应用。我已经构建了函数,但无法弄清楚如何访问确切的json值,即“ 5. Exchange Rate”。
以下是一些代码和json数据,可以帮助您更好地进行解释:
内置的URL:
workdir: config['workdir']
视图控制器中的行情
func USDtoEUR(_ completionHandler: @escaping (_ success: Bool, _ quotes: [String:AnyObject]?, _ error: String?) -> Void) {
let urlString = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=EUR&apikey=NP3M8LL62YJDO0YX"
let session = URLSession.shared
let url = URL(string: urlString)!
let request = URLRequest(url: url)
let task = session.dataTask(with: request, completionHandler: { data, response, error in
if error != nil {
completionHandler(false, nil, error!.localizedDescription)
}
else {
do {
let result = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
if let dictionary = result["Realtime Currency Exchange Rate"] as? [String:AnyObject]! {
completionHandler(true, dictionary, nil)
}
else {
completionHandler(false, nil, nil)
}
} catch {
completionHandler(false, nil, "Unable to process retrieved data.")
}
}
})
task.resume()
}
//触摸“ USD”按钮后显示的报价:
func usdQUotesRequest() {
USDClient().USDtoEUR() { success, newQuote, error in
if success {
self.usdtoeurquote = newQuote
DispatchQueue.main.async {
self.stopActivityIndicator()
self.Refresh.isEnabled = true
}
} else {
DispatchQueue.main.async {
self.displayAlert("Unable to Retrieve Latest Conversion Rates", message: "\(error!)")
self.stopActivityIndicator()
self.Refresh.isEnabled = true
}
}
}
原始JSON数据:The json data on the webpage
答案 0 :(得分:1)
主要错误是键5. Exchange Rate
的值为String
而不是Float
,并且在强行打开包装时,代码可靠地崩溃了。
在Swift 4中,Decodable
协议比字典方便得多。
创建两个结构。 ExchangeRate
结构包含一个计算属性floatRate
以返回Float
值
struct Root : Decodable {
let exchangeRate : ExchangeRate
private enum CodingKeys: String, CodingKey { case exchangeRate = "Realtime Currency Exchange Rate" }
}
struct ExchangeRate : Decodable {
let fromCode, fromName, toCode, toName, rate : String
let lastRefreshed, timeZone : String
private enum CodingKeys: String, CodingKey {
case fromCode = "1. From_Currency Code"
case fromName = "2. From_Currency Name"
case toCode = "3. To_Currency Code"
case toName = "4. To_Currency Name"
case rate = "5. Exchange Rate"
case lastRefreshed = "6. Last Refreshed"
case timeZone = "7. Time Zone"
}
var floatRate : Float {
return Float(rate) ?? 0.0
}
}
然后通过将 from 和传递给货币作为参数,使下载功能更加通用。此版本使用JSONDecoder
并返回ExchangeRate
实例或Error
func downloadRate(from: String, to: String, completionHandler: @escaping (ExchangeRate?, Error?) -> Void) {
let urlString = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=\(from)&to_currency=\(to)&apikey=NP3M8LL62YJDO0YX"
let task = URLSession.shared.dataTask(with: URL(string: urlString)!, completionHandler: { data, response, error in
if error != nil {
completionHandler(nil, error!)
} else {
do {
let result = try JSONDecoder().decode(Root.self, from: data!)
completionHandler(result.exchangeRate, nil)
} catch {
completionHandler(nil, error)
}
}
})
task.resume()
}
并使用它
func usdQUotesRequest() {
USDClient().downloadRate(from:"USD", to: "EUR") { exchangeRate, error in
if let exchangeRate = exchangeRate {
self.usdtoeurquote = exchangeRate.floatRate
DispatchQueue.main.async {
self.stopActivityIndicator()
self.Refresh.isEnabled = true
}
} else {
DispatchQueue.main.async {
self.displayAlert("Unable to Retrieve Latest Conversion Rates", message: "\(error!)")
self.stopActivityIndicator()
self.Refresh.isEnabled = true
}
}
}