2016-11-18 75 views
0

我正在製作一個計算器應用程序,用於解決缺少的變量,並添加了一段代碼,通過點擊屏幕上的任意位置來消除鍵盤或按下「返回」鍵。線程1:EXC_BAD_INSTRUCTION關閉鍵盤和零值的問題(.isNaN)

在我添加if else語句來檢查哪個變量丟失(使用.isNaN)以便它可以解決該缺失的變量之前,此函數正在工作。

這裏是我的那個特定視圖控制器代碼:

import UIKit 

class findSpeed: UIViewController, UITextFieldDelegate { 


@IBOutlet weak var vValue: UITextField! 
@IBOutlet weak var dValue: UITextField! 
@IBOutlet weak var tValue: UITextField! 
@IBOutlet weak var answer: UILabel! 

@IBAction func solve(_ sender: UIButton) { 

    let v = Float(vValue.text! as String); 
    let d = Float(dValue.text! as String); 
    let t = Float(tValue.text! as String); 

    let solvev = (d!/t!); 
    let solved = (v!*t!); 
    let solvet = (d!/v!); 

    if (v!.isNaN) { 
     answer.text = "Answer: \(solvev)" 
    } 

    else if (d!.isNaN) { 
     answer.text = "Answer: \(solved)" 
    } 

    else { 
     answer.text = "Answer: \(solvet)" 
    } 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    vValue.delegate = self; 
    dValue.delegate = self; 
    tValue.delegate = self; 
    self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("dismiss"))) 
} 

func dismiss() { 
    vValue.resignFirstResponder() 
    dValue.resignFirstResponder() 
    tValue.resignFirstResponder() 
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    vValue.resignFirstResponder() 
    dValue.resignFirstResponder() 
    tValue.resignFirstResponder() 
    return true 
} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

當我點擊鏈接到該視圖控制器,其中計算器按鈕我收到的錯誤發生時: Picture of error

+0

僅供參考 - 您的問題與解除鍵盤無關,並且與任何添加的「else」語句無關。你的代碼在'viewDidLoad'中崩潰,那麼怎麼解決'solve'中的任何代碼? – rmaddy

回答