Xcode 跳过 if else 语句

Xcode skips if else statement

我的 if else 语句检查某些文本字段是否为空,如果是则弹出警报。然而 xcode 即使经历了所有事情,也会转向其他功能。

有一个 if 语句检查分段控件的值并相应地检查一些文本字段。

@IBAction func calc(_ sender: Any) {
    
    // Check if dilution text field is empty
    let dilutiontext = self.dilution.text
    if (dilutiontext?.isEmpty ?? true) {
        Alert.showAlert(on: self, with: "Empty Fields", message: "Dilution field is empty")
    }
    if choose.selectedSegmentIndex == 0 {
        
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true {
            Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-4 fields should not be empty")
        } else {
            performSegue(withIdentifier: "turner", sender: self)
        }
    } else {
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true {
            Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-8 fields should not be empty")
        } else {
            performSegue(withIdentifier: "turner", sender: self)
        }
    }
    
}

我有另一个文件 alert.swift 控制警报:

import Foundation
import UIKit

struct Alert {
    public static func showAlert(on vc: UIViewController, with title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        vc.present(alert, animated: true)
    }
}

编辑:

以前 self.dilution.text?.isEmpty 现在让 dilutiontext = self.dilution.text 和 dilutiontext?isEmpty

我注释掉了 prepare for segue 功能,令人惊讶的是警报开始工作了。我仍然需要该功能和警报。这是函数:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    var vc = segue.destination as! SecondViewController
    
    if choose.selectedSegmentIndex == 0 {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.dil = Int(dilution.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)

    } else {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.n5 = Int(number5.text!)!
        vc.n6 = Int(number6.text!)!
        vc.n7 = Int(number7.text!)!
        vc.n8 = Int(number8.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)
        vc.dil = Int(dilution.text!)!
    }

}

当我 运行 它时,它没有显示警报(检查文本字段是否为空),而是继续到 segue 函数并在展开可选值时显示意外发现 nil,这是预期的

显然“if”和“else if”条件都不成立。添加

let dilutiontext = self.dilution.text
let celltext = self.cell.text

然后设置断点并检查值。

很明显,如果 segue 函数中的 if 条件之一为真,则会跳过警报。因此,如果有什么东西最初会使陈述为假,然后在通过警报后使它们为真,那么问题就解决了。

因此我为 segue 函数中的 if 和 if else 语句分别创建了两个函数。

func option1() -> Bool {
    if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true || !(self.number5.text?.isEmpty ?? true) || !(self.number6.text?.isEmpty ?? true) || !(self.number7.text?.isEmpty ?? true) || !(self.number8.text?.isEmpty ?? true) {
        return false
    } else {
        return true
    }
}

func option2() -> Bool {
    if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true {
        return false
    } else {
        return true
    }
}

检查所有条件是否为真,如果为真,return为真,以便程序可以继续执行 segue 函数。

segue 将检查条件是否为真,如果不是,它将通过 alerts-therefore 使 option1() 或 option2() return 为真 - 因此 if 条件在segue func 对于程序继续是正确的。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    var vc = segue.destination as! SecondViewController

    if option1() == true {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.dil = Int(dilution.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)

    } else if option2() == true {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.n5 = Int(number5.text!)!
        vc.n6 = Int(number6.text!)!
        vc.n7 = Int(number7.text!)!
        vc.n8 = Int(number8.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)
        vc.dil = Int(dilution.text!)!
    }

}