0

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

 @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)"
        }
4

2 回答 2

1

您的if语句检查是否所有值都是nil. 你想看看他们中的任何一个是nil.

更改if为:

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

我没有在 Swift 中编程,但我的印象是你应该做很多其他改进来验证值不是nil.

于 2016-07-02T00:06:33.083 回答
0

在 Swift 中,您应该仔细考虑是要强制解包(!)一个值还是有条件地解包一个值(?) - 因为强制解包 anil会给您一个运行时异常。

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

let a = Double(textFieldA.text!)

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

一旦你有了可选项,你就可以使用 Swiftguard语句来检查 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()返回一个可选的 Double,两个 Double 相加的结果是一个 Double,因此您无需声明该常量将是一个 Double。

于 2016-07-02T00:34:03.327 回答