swift - Swift4 中的 completionHandler 返回字符串

标签 swift asynchronous completionhandler

我正在尝试构建一个小型货币转换器,但问题是我的 completionHandler 不起作用。因此,函数执行后输入的 Currency 不会立即改变

我已经尝试实现一个 completionHandler;然而,还没有成功

class CurrencyExchange: ViewController {

    //Outlets
    @IBOutlet weak var lblCurrency: UILabel!
    @IBOutlet weak var segOutputCurrency: UISegmentedControl!
    @IBOutlet weak var txtValue: UITextField!
    @IBOutlet weak var segInputCurrency: UISegmentedControl!


    //Variables
    var inputCurrency: String!
    var currencyCNY: Double!
    var currencyEUR: Double!
    var currencyGBP: Double!
    var currencyJPY: Double!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.isNavigationBarHidden = true
    }


    @IBAction func btnConvert(_ sender: Any) {
        assignOutput()

        if txtValue.text == "" {
            self.lblCurrency.text = "Please insert value"
        } else {
            let inputValue = Double(txtValue.text!)!
            if segOutputCurrency.selectedSegmentIndex == 0  {
                    let output = Double(inputValue * currencyCNY!)
                    self.lblCurrency.text = "\(output)¥"
            }  else if  segOutputCurrency.selectedSegmentIndex == 1 {
                let output = Double(inputValue * currencyEUR!)
                self.lblCurrency.text = "\(output)€"
            }  else if  segOutputCurrency.selectedSegmentIndex == 2 {
                let output = Double(inputValue * currencyGBP!)
                self.lblCurrency.text = "\(output)"
            } else if  segOutputCurrency.selectedSegmentIndex == 3 {
                let output = Double(inputValue * currencyJPY!)
                self.lblCurrency.text = "\(output)"
            }
        }
    }





    func assignOutput() {

        let currencies = ["EUR", "JPY",  "CNY", "USD"]
        inputCurrency = currencies[segInputCurrency.selectedSegmentIndex]


        Alamofire.request("https://api.exchangeratesapi.io/latest?base=\(inputCurrency!)").responseJSON { (response) in
            let result = response.result
            let jsonCurrencies = JSON(result.value!)
            let dictContent = jsonCurrencies["rates"]
            self.currencyCNY = dictContent["CNY"].double
            self.currencyEUR = dictContent["EUR"].double
            self.currencyGBP = dictContent["GBP"].double
            self.currencyJPY = dictContent["JPY"].double
        }
    }   
}

预期的结果是每次调用 btnConvert 函数时都会调用 assignInput 和 assignOutput 函数并将变量设置为正确的值。我是初学者,因此非常感谢任何帮助。

最佳答案

您需要在assignOutput() 中完成处理程序,我还添加了最小错误处理以避免崩溃

//Variables
var inputCurrency = ""
var currencyCNY = 0.0
var currencyEUR = 0.0
var currencyGBP = 0.0
var currencyJPY = 0.0

@IBAction func btnConvert(_ sender: Any) {
    assignOutput() { success in 
        if success {
            if txtValue.text!.isEmpty {
                self.lblCurrency.text = "Please insert value"
            } else {
                if let inputValue = Double(txtValue.text!) { 
                    if segOutputCurrency.selectedSegmentIndex == 0  {
                        let output = Double(inputValue * currencyCNY)
                        self.lblCurrency.text = "\(output)¥"
                    }  else if  segOutputCurrency.selectedSegmentIndex == 1 {
                        let output = Double(inputValue * currencyEUR)
                        self.lblCurrency.text = "\(output)€"
                    }  else if  segOutputCurrency.selectedSegmentIndex == 2 {
                        let output = Double(inputValue * currencyGBP)
                        self.lblCurrency.text = "\(output)"
                    } else if  segOutputCurrency.selectedSegmentIndex == 3 {
                        let output = Double(inputValue * currencyJPY)
                        self.lblCurrency.text = "\(output)"
                    }
                } else {
                   self.lblCurrency.text = "Please enter a number"
                }
           }
        } else {
            self.lblCurrency.text = "Could not receive the exchange rates"
        }
    }
}

func assignOutput(completion: @escaping (Bool) -> Void) {

    let currencies = ["EUR", "JPY",  "CNY", "USD"]
    inputCurrency = currencies[segInputCurrency.selectedSegmentIndex]

    Alamofire.request("https://api.exchangeratesapi.io/latest?base=\(inputCurrency)").responseJSON { (response) in
        if let result = response.result.value {
            let jsonCurrencies = JSON(result)
            let dictContent = jsonCurrencies["rates"]
            self.currencyCNY = dictContent["CNY"].double
            self.currencyEUR = dictContent["EUR"].double
            self.currencyGBP = dictContent["GBP"].double
            self.currencyJPY = dictContent["JPY"].double
            completion(true)
        } else {
            completion(false)
        }
    }
}   

关于swift - Swift4 中的 completionHandler 返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56210626/

相关文章:

Swift,如何让 Completion Handler 按我的意愿工作?

ios - 在完成 block Swift 中返回

ios - 设置 Swift-Clean 以使用其功能

ios - RestKit RKObjectMapping Swift 可选

ios - 将 UIView 转换到 SKView 时线程 1 信号 SIGABRT 崩溃

swift - 通过 self 更新闭包内的 UI 是一种不好的做法吗?

ios - UISearchController 的 UISearchBar 的光标显示第一次使用,而不是后续

javascript - 异步函数调用后从工厂返回 Angular $resource

javascript - 在javascript中终止挂起的 promise

Android Retrofit 2 等待多个请求