gpt4 book ai didi

ios - 错误 : Optional(Alamofire. AFError.invalidURL (""))

转载 作者:行者123 更新时间:2023-12-01 22:14:19 25 4
gpt4 key购买 nike

我刚开始用 Swift 编写代码,在使用 Alamofire 构建应用程序时遇到了这个错误

错误是:

Error: Optional(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"
}
}
}

最佳答案

问题是您正在调用 getCurrencyData从您的 View Controller viewDidLoad方法,而 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 为其分配数据而不是 currencySymbolArray .

关于ios - 错误 : Optional(Alamofire. AFError.invalidURL ("")),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52502572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com