I am trying to update labels with data fetched from an API to print Bitcoin price and percentage variation in an app but I can't figure out how to properly decode the JSON.
BitcoinInfo.swift:
import Foundation
import UIKit
struct Bitcoin: Codable {
let percentChange1h: String
let priceEUR: String
private enum CodingKeys: String, CodingKey {
case percentChange1h = "percent_change_1h", priceEUR = "price_eur"
}
}
extension Bitcoin {
var priceEURdecimal: Decimal {
return Decimal(string: priceEUR) ?? 0
}
var priceEURcurrency: String {
Formatter.currency.locale = Locale(identifier: "fr_FR")
return Formatter.currency.string(for: priceEURdecimal) ?? ""
}
}
ViewController.swift:
import Foundation
import UIKit
class ViewController: UIViewController {
let bitcoinInfoController = BitcoinInfoController()
@IBOutlet weak var bitcoinPriceLabel: UILabel!
@IBOutlet weak var bitcoinPercentageChangeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
bitcoinPriceLabel.text = ""
bitcoinPercentageChangeLabel.text = ""
fetchBitcoinInfo { bitcoin, error in
guard let bitcoin = bitcoin else {
print(error!);
return
}
self.updateUI(with: bitcoin)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func updateUI(with bitcoinInfo: Bitcoin) {
DispatchQueue.main.async {
self.bitcoinPriceLabel.text = bitcoinInfo.priceEURcurrency
self.bitcoinPercentageChangeLabel.text = String(format: "%.2f%%", Double(bitcoinInfo.percentChange1h) ?? 0)
}
}
func fetchBitcoinInfo(completion: @escaping (Bitcoin?, Error?) -> Void) {
let baseURL = URL(string: "https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR")!
let url = baseURL
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
if let bitcoinEUR = try JSONDecoder().decode([Bitcoin].self, from: data).first {
print(bitcoinEUR)
print(bitcoinEUR.priceEUR)
print(bitcoinEUR.priceEURdecimal)
print(bitcoinEUR.priceEURcurrency)
print(bitcoinEUR.percentChange1h)
completion(bitcoinEUR, nil)
}
} catch {
print(error)
}
}
task.resume()
}
}
The console is printing "Either no data was returned or data was not properly decoded."
EDIT: This is the latest version of the code. EDIT2: The code is now 100% functional! :)
try?
– catch the error and print it out; it’ll tell you exactly what the problem is. – Hamish