Я только начинаю кодировать с помощью Swift, и я столкнулся с этой ошибкой при создании приложения с использованием Alamofire
Ошибка:
Ошибка: необязательная (Alamofire.AFError.invalidURL (""))
Это мой код.Может кто-нибудь помочь ??
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let baseURL = "https://apiv2.bitcoinaverage.com/indices/global/ticker/BTC"
let currencyArray = ["AUD", "BRL","CAD","CNY","EUR","GBP","HKD","IDR","ILS","INR","JPY","MXN","NOK","NZD","PLN","RON","RUB","SEK","SGD","USD","ZAR"]
let currencySymbolsArray = ["$", "R$", "$", "¥", "€", "£", "$", "Rp", "₪", "₹", "¥", "$", "kr", "$", "zł", "lei", "₽", "kr", "$", "$", "R"]
var currencySymbol = ""
var finalURL = ""
//Pre-setup IBOutlets
@IBOutlet weak var bitcoinPriceLabel: UILabel!
@IBOutlet weak var currencyPicker: UIPickerView!
// determine number of columns
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// determine number of rows
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return currencyArray.count
}
// fill in each row with title from array
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return currencyArray[row]
}
// Print something when you select a row
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print (currencyArray[row])
finalURL = baseURL + currencyArray[row]
print(finalURL)
currencySymbol = currencyArray[row]
}
override func viewDidLoad() {
super.viewDidLoad()
currencyPicker.delegate = self
currencyPicker.dataSource = self
getCurrencyData(url: finalURL)
}
//TODO: Place your 3 UIPickerView delegate methods here
// //MARK: - Networking
// /***************************************************************/
func getCurrencyData(url: String) {
Alamofire.request(url, method: .get)
.responseJSON { response in
if response.result.isSuccess {
print("Success! Got the currency data ")
let currencyJSON : JSON = JSON(response.result.value!)
self.updateCurrencyData(json: currencyJSON)
} else {
print("Error: \(String(describing: response.result.error))")
self.bitcoinPriceLabel.text = "Connection issues"
}
}
}
// //MARK: - JSON Parsing
// /***************************************************************/
func updateCurrencyData(json : JSON) {
if let currencyResult = json["ask"].double {
bitcoinPriceLabel.text = " \(currencySymbol) + \(currencyResult) "
} else {
bitcoinPriceLabel.text = "Currency unavailable"
}
}
}