I'm trying to show the price in dollars of a cryptocoin called Litecoin.
I'm already using an API for Bitcoin, everything about it is working perfectly.
I found an API for access Litecoin price.
https://www.worldcoinindex.com/apiservice
I created my key and now I'm using the url to access the information.
That's my code.
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
let bitcoinURL = "https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD"
let litecoinURL = "https://www.worldcoinindex.com/apiservice/ticker?key=NmII3h4FFwacc4HpKftSxfVuLsGzmc&label=ethbtc-ltcbtc&fiat=usd"
@IBOutlet weak var bitcoinValue: UILabel!
@IBOutlet weak var litecoinValue: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
getCryptoCoinData(url: bitcoinURL)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getCryptoCoinData(url: String) {
Alamofire.request(url, method: .get).responseJSON { response in if response.result.isSuccess {
let bitcoinJSON : JSON = JSON(response.result.value ?? "")
let litecoinJSON: JSON = JSON(response.result.value ?? "")
self.updateBitcoinData(json: bitcoinJSON)
self.updateLitecoinData(json: litecoinJSON)
} else {
print("Error: \(String(describing: response.result.error))")
self.bitcoinValue.text = "Connection Issues"
self.litecoinValue.text = "Connection Issues"
}
}
}
func updateBitcoinData(json : JSON) {
if let bitcoinResult = json["ask"].double {
bitcoinValue.text = String(bitcoinResult)
} else {
bitcoinValue.text = "Price unavailable"
}
}
func updateLitecoinData(json: JSON) {
if let litecoinResult = json["Label"][0]["Price"].double {
litecoinValue.text = String(litecoinResult)
} else {
litecoinValue.text = "Unavailable"
}
}
}
API example:
{
"Markets" : [
{
"Label" : "ETH/BTC",
"Name" : "Ethereum",
"Price" : 0.01948437,
"Volume_24h" : 28680.92498425,
"Timestamp" : 1461221820
},{
"Label" : "LTC/BTC",
"Name" : "Litecoin",
"Price" : 0.01948437,
"Volume_24h" : 28680.92498425,
"Timestamp" : 1461221820
}
]
}
The problem occours in line 59. The constant litecoinResult never gets a value.
No matter what I try to do, my label "litecoinValue" just shows the message "Unavailable".