我正在使用Coinbase API来获取比特币的现货,买卖价格。我添加了一个包含所有支持货币的选择器视图,当我更改货币时,现货价格会发生变化,但买入和卖出的价格却没有变化。这是我的代码,不知道为什么不工作......
@IBOutlet weak var btcSpotPrice: UILabel!
@IBOutlet weak var btcBuyPrice: UILabel!
@IBOutlet weak var btcSellPrice: UILabel!
@IBOutlet weak var selectedCurrency: UIButton!
@IBOutlet weak var currencyPicker: UIPickerView!
var currencies: [CoinbaseCurrency]?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
self.selectedCurrency.setTitle(currencies?[row].iso, for: .normal)
getSpotPrice()
getBuyPrice()
getSellPrice()
}
func getSpotPrice()
{
Coinbase().getSpotRate(withCurrency: self.selectedCurrency.currentTitle) { (spotPrice: CoinbaseBalance?, error: Error?) in
if let error = error
{
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.present(alert, animated: true, completion: nil)
}
else
{
self.btcSpotPrice.text = spotPrice!.amount! + " " + spotPrice!.currency!
}
}
}
func getBuyPrice()
{
Coinbase().getBuyPrice(withQuantity: "1", currency: "GBP") { (btc: CoinbaseBalance?, fees: [Any]?, subtotal: CoinbaseBalance?, total: CoinbaseBalance?, error: Error?) in
if let error = error
{
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.present(alert, animated: true, completion: nil)
}
else
{
self.btcBuyPrice.text = total!.currency!
}
}
}
func getSellPrice()
{
Coinbase().getSellPrice(withQuantity: "1", currency: self.selectedCurrency.currentTitle) { (btc: CoinbaseBalance?, fees: [Any]?, subtotal: CoinbaseBalance?, total: CoinbaseBalance?, error: Error?) in
if let error = error
{
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
alert.present(alert, animated: true, completion: nil)
}
else
{
self.btcSellPrice.text = total!.amount!
}
}
}
在我的getBuyPrice方法中,即使将货币硬编码为“GBP”英镑,btcBuyPrice标签仍显示美元。它适用于spotPrice方法,但不适用于买卖价格。