如何在不退出Swift代码的情况下使用多个guard(或if let)语句?

时间:2017-07-09 10:41:41

标签: swift

我尝试根据几个文本字段的输入执行简单的数学计算。如果没有填写一个字段,我想继续计算其他字段。但是,这不适用于我使用(不恰当的?)guard语句。

简单地说:

如果填写了field1,则执行计算(即将该数量乘以0.5)并存储结果。

如果填写了field2,则执行计算(即将该数量乘以0.2)并存储结果。

作为绝对的初学者,我试过这个:

@IBAction func calculate(_ sender: Any) {
    //amount 1
    guard let amount1 = textField1.text else{
        return}
    guard let amount1double = Double(amount1) else{
        return}

    var amt1 = amount1double
    amt1 = amt1 * 0.5

    //amount 2
    guard let amount2 = textField2.text else{
        return }

    guard let amount2double = Double(amount2) else{
        return }

    var amt2 = amount2double
    amt2 = amt2 * 0.2
}

如果未填写textField1,则上述代码将停止。但是,如果textField1为空,我希望代码继续计算amount2。

我在texfField中的if let之后读到了nil语句可能会继续使用其余的代码,但是我无法使其工作。此外,我知道这只允许我在该块中访问该值。

目标是最终获得所有计算金额的最高/最高金额(如果有)。

3 个答案:

答案 0 :(得分:1)

我尝试使用if-let解决您的问题。检查以下代码。

var amt1 : Double = 0.0
        if let amount1 = textfield1.text{
            if let amount1InDouble = Double(amount1){
                amt1 = amount1InDouble
                amt1 = amt1 * 0.5
            }
        }

        var amt2 : Double = 0.0
        if let amount2 = textfield2.text{
            if let amount2InDouble = Double(amount2){
                amt2 = amount2InDouble
                amt2 = amt2 * 0.2
            }
        }

答案 1 :(得分:0)

更新

您不需要使用guardif-let。您可以使用默认值let

@IBAction func calculate(_ sender: Any) {
    let amount1double = Double(textField1.text ?? "") ?? 0 // 0 is default value, Change it to whatever you want.
    let amount2double = Double(textField2.text ?? "") ?? 0 // 0 is default value, Change it to whatever you want.

    var amt1 = amount1double
    amt1 = amt1 * 0.5

    var amt2 = amount2double
    amt2 = amt2 * 0.5
}

使用 if let

@IBAction func calculate(_ sender: Any) {    
    if let amount1 = textField1.text,
        let amount2 = textField2.text,
        let amount1double = Double(amount1),
        let amount2double = Double(amount2) {

        var amt1 = amount1double
        amt1 = amt1 * 0.5

        var amt2 = amount2double
        amt2 = amt2 * 0.5
    }
}

使用 guard

@IBAction func calculate(_ sender: Any) {    
    guard let amount1 = textField1.text,
        let amount2 = textField2.text,
        let amount1double = Double(amount1),
        let amount2double = Double(amount2)  else {
        return
    }

    var amt1 = amount1double
    amt1 = amt1 * 0.5

    var amt2 = amount2double
    amt2 = amt2 * 0.5
}

答案 2 :(得分:0)

你可以这样做:

@IBAction func calculate(_ sender: Any) {
    //amount 1
    let amount1double = calculateAmountTxt1()
    //amount 2
    let amount2double = calculateAmountTxt2()

    var amt1 = amount1double
    amt1 = amt1 * 0.5

    var amt2 = amount2double
    amt2 = amt2 * 0.2
}

func calculateAmountTxt1()->CGFloat{
     //amount 1
    guard let amount1 = textField1.text, let amount1double = Double(amount1)else {
       return 0
    }
    return amount1double
}

func calculateAmountTxt2()->CGFloat{
    //amount 2
    guard let let amount2 = textField2.text,let amount2double = Double(amount2)else {
       return 0
    }
    return amount2double
}