2017-05-28 8 views
0

HourGoalおよびMinuteGoalテキストフィールドが空の場合にコードを認識させることができません。彼らはゼロ値を持っているか、空であると決して上がらず、値をアンラップしようとするとプログラムがクラッシュします。わかりました...は空白としてテキストフィールドを認識しません

import UIKit 

class ReadingGoal: UIViewController { 
    @IBOutlet weak var HourGoal: UITextField! 
    @IBOutlet weak var MinuteGoal: UITextField! 
    @IBOutlet weak var NextButton: UIButton! 
    var mingoal = Int() 
    var hrgoal = Int() 
    var secondstoread:Int = 0 
    var initialtime:Int = Int() 

    @IBAction func HourEditingDidChange(_ sender: Any) { 
     NextButton.isEnabled = true 
    } 

    @IBAction func MinuteEditingDidChange(_ sender: Any) { 
     NextButton.isEnabled = true 
    } 

    func disable() { 
     NextButton.isEnabled = false 
    } 

    @IBAction func NextButtonIsPressed(_ sender: Any) { 
     print("GERALDINE IS AWESOME!!!!!") 

     // ERROR 
     if HourGoal.text?.isEmpty == false || MinuteGoal.text?.isEmpty == false { 
      if HourGoal.text != nil && HourGoal.text?.isEmpty == false{ 
      // self.hrgoal = Int(HourGoal.text!)! 
       print("hr Value exists") 
       print("\(HourGoal.text!)") 
      } 
      else { 
       NextButton.isEnabled = false 
      } 
      if MinuteGoal.text != nil && MinuteGoal.text?.isEmpty == false{ 
       // self.mingoal = Int(MinuteGoal.text!)! 
       print("min value exists") 
       print("\(MinuteGoal.text!)") 
      } 
      else { 
       NextButton.isEnabled = false 
      } 
      print("in ") 
      // secondstoread = ((mingoal + (hrgoal*60))*60) 
      secondstoread = 10 

      if secondstoread > 0 { 
       performSegue(withIdentifier: "Mr.Friar-DavisIsAGiantBallOfFloof", sender: self) 
      } 
      else { 
       NextButton.isEnabled = false 
      } 
      //function saves text field info to core data 
     } 
    } 

    //for the specific segue 'toBookInfo', certain information is passed through the segue 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     //making sure we are using correct segue 
     if segue.identifier == "Mr.Friar-DavisIsAGiantBallOfFloof" { 

       //selecting the destination controller 
       let detailVC: TimerController = segue.destination as! TimerController 

       //sending properties to destination view controller 
       detailVC.seconds = secondstoread 
      } 
    else { 
      // Error sender is not a cell or cell is not in collectionView. 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     initialtime = secondstoread 
    } 
} 

答えて

0

は、ここでの用語教えることはありませんが、すべての変数は、この、さえIBOutletsよう小文字で始める必要があります...

私はしばらくの間、あなたのコードを勉強:

どのような費用で除外マークを使用しないでください! - >あなたが使っているためにたくさんクラッシュする理由!どこでも、その力は選択肢を展開し、迅速な美しさを完全に破壊します。

このラインは、コンパイルオプションで問題なく動作するのはどうですか?

if HourGoal.text?.isEmpty == false || MinuteGoal.text?.isEmpty == false 

あなたが何らかの形で任意の値をチェックすると、コードの行が提供する前に値が、間違いなくnilである:

guard let hourGoalText = HourGoal.text, let minuteGoal = minuteGoal.text else { //handle code for empty textField 
return } 

これはoptionalsをチェックするための最初のステップです...今のコードは、」勝ちましたトンクラッシュ...しかし、正直なところ

をデバッグすることなく、あなたはガードを使用している場合みましょうか、聞かせての文は、あなたが

0

をクラッシュとのトラブルを持っていない場合は、を確認することができます空虚になります。それらのいくつかは以下のとおりです。

if textField.text?.isEmpty ?? true{ 
    print("textField is empty") 
} 
else{ 
     print("textField has some text") 
} 

または

if textfield.text?.characters.count==0 { 
    print("textField is empty") 
} 
else{ 
    print("textField has some text") 
} 

または

if textfield.text == "" { 
    print("textField is empty") 
} 
else{ 
    print("textField has some text") 
} 
0

は安全な方法でアンラップのためif letまたはguardを使用してください。この

if let texthour = HourGoal.text{ 
     // has text and can be "", next check for blank string 
     if texthour == ""{ 

    } 
} 

ORドミニクとしてguardを使用するよう 何かが

を示し
関連する問題