Ios 错误:可选(Alamofire.AFError.invalidURL(";)
我刚开始使用Swift编码,在使用Alamofire构建应用程序时遇到了这个错误 错误是: 错误:可选(Alamofire.AFError.invalidURL(“”) 这是我的密码。有人能帮忙吗Ios 错误:可选(Alamofire.AFError.invalidURL(";),ios,swift,alamofire,Ios,Swift,Alamofire,我刚开始使用Swift编码,在使用Alamofire构建应用程序时遇到了这个错误 错误是: 错误:可选(Alamofire.AFError.invalidURL(“”) 这是我的密码。有人能帮忙吗 import UIKit import Alamofire import SwiftyJSON class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { let base
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"
}
}
}
问题是您正在从视图控制器
viewDidLoad
方法调用getCurrencyData
,而finalURL
的值仍然是它的初始值,即“
,这正是您在错误消息中看到的无效URL
您只需要在用户实际使用选择器进行选择后进行网络调用
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
currencySymbol = currencySymbolsArray[row]
getCurrencyData(url: baseURL + currencyArray[row])
}
也不需要使用finalURL
变量,因为您可以直接将存储在finalURL
中的数据输入到getCurrencyData
函数中
与错误无关,但您也将错误数据设置为
currencySymbol
,因为您是从currencyArray
而不是viewDidLoad()
中调用getCurrencyData
的currencySymbolArray为其分配数据。问:在这种情况下,finalur
的价值是什么?当您打印(最终版)
时,它是否有效?如果您在Safari.app或任何其他浏览器中复制/粘贴它,它是否有效?也是完全相同的(没有额外的百分比转义%added?)是的,地址有效。这两个示例都以https://(由于某种原因,我在复制/粘贴时没有出现)开头。不,就我所知,没有额外的%,让我们再次澄清一下:在viewDidLoad()
中,就在getCurrencyData(url:finalURL)
之前,finalURL
是“
,不是吗?这就是为什么,不是吗?五星级!!这是有道理的,而且成功了!!