在我的方程求解程序中,输入 nil 值会使应用程序崩溃

In my equation solving program, inputting nil value crashes app

我已经制作了一个求解二次方程的应用程序(只是想看看我是否可以),但是,如果用户不小心为其中一个槽输入任何值,我不希望该应用程序崩溃。请帮忙。

 @IBAction func solveButton(sender: AnyObject) {

        let a:Double! = Double(textFieldA.text!)      // textfieldA is UITextField
        let b:Double! = Double(textFieldB.text!)    // textfieldB is UITextField
        let c:Double! = Double(textFieldC.text!)   // textFieldC is UITextField

        // This is for if the a value is nil
        if a == nil && b == nil && c == nil{

            errorTextField.text = "Please do not leave an empty value, all quadratic equations must have 3 values!"
            answerLabel.text = " "

            answerLabelNegative.text = " "
            whatEquation.text = " "
            return

        } else {

            let a:Double! = Double(textFieldA.text!)      // textfieldA is UITextField
            let b:Double! = Double(textFieldB.text!)    // textfieldB is UITextField
            let c:Double! = Double(textFieldC.text!)   // textFieldC is UITextField
            let x:Double = (b * b)
            let y:Double = -4 * a * c //Here I get EXC_BAD_INSTRUCTION error
            let xy:Double = x + y
            let az:Double = sqrt(xy)
            let finalTopPlus:Double = -b + az
            let finalTopMinus:Double = -b - az
            let aTwo:Double = 2 * a
            let finalEquationPositive:Double = finalTopPlus / aTwo
            let finalEquationNegative:Double = finalTopMinus / aTwo

            answerLabel.text = "Positive x = \(finalEquationPositive)"
            answerLabelNegative.text = "Negative x = \(finalEquationNegative)"

            let aValue = a < 0 ? "-" : " "
            let bValue = b < 0 ? " " : "+"
            let cValue = c < 0 ? " " : "+"
            whatEquation.text = "Equation: \(aValue) \(a)x² \(bValue) \(b)x \(cValue) \(c)"
        }

您的 if 语句检查是否所有值都是 nil。您想查看其中是否有 nil.

if 更改为:

if a == nil || b == nil || c == nil {

我没有在 Swift 中编程,但我的印象是您应该进行许多其他改进以验证值不是 nil

在 Swift 中,您应该仔细考虑是要强制展开 (!) 值还是有条件地展开值 (?) - 因为强制展开 nil 会给您 运行时间例外。

例如,取你的第一行。这可能应该是:

let a = Double(textFieldA.text!)

这将导致 a 成为 Double? - 即它是可选的,因此它可能为零。可以强制展开 textFieldA.text 值,因为这将由您的故事板设置(或者您创建文本字段),这里的 nil 值表示您需要调查和解决的严重错误。

一旦有了可选项,就可以使用 Swift guard 语句来检查是否为 nil。 使用 guard 语句后,您可以安全地使用 !强制展开值,因为您知道它们不是零。

@IBAction func solveButton(sender: AnyObject) {

    guard let a = Double(textFieldA.text!), 
          let b = Double(textFieldB.text!),
          let c = Double(textFieldC.text!) else {
        errorTextField.text = "Please do not leave an empty value, all quadratic equations must have 3 values!"
        answerLabel.text = " "
        answerLabelNegative.text = " "
        whatEquation.text = " "
        return
    }

    let x = (b * b)
    let y = -4 * a * c 
    let xy = x + y
    let az = sqrt(xy)
    let finalTopPlus= -b + az
    let finalTopMinus = -b - az
    let aTwo= 2 * a
    let finalEquationPositive = finalTopPlus / aTwo
    let finalEquationNegative = finalTopMinus / aTwo
    answerLabel.text = "Positive x = \(finalEquationPositive)"
    answerLabelNegative.text = "Negative x = \(finalEquationNegative)"

    let aValue = a < 0 ? "-" : " "
    let bValue = b < 0 ? " " : "+"
    let cValue = c < 0 ? " " : "+"
    whatEquation.text = "Equation: \(aValue) \(a)x² \(bValue) \(b)x \(cValue) \(c)"
}

请注意,在Swift中,如果无法推断,您只需要定义变量或常量的类型。 Double() returns 一个可选的 Double,两个 Double 相加的结果是一个 Double,因此您无需声明常量将是一个 Double。