-3

I've been looking on this site for an answer to my question but no matter what I do my compiler still gives me the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

I don't know what is causing this seeing as I wrapped up all my code in an if statement to ensure that if the value submitted is nil it would print a message.

Here is the code:

@IBOutlet var textField: UITextField!

@IBOutlet var label4: UILabel!

@IBAction func buttonTapped(_ sender: Any) {

    if textField.text != nil {
        let textFieldInt: Int? = Int(textField.text!)
        let convert = textFieldInt! * 7
        label4.text = String(convert)
    }

    else {
    label4.text = "Please enter a number!"
    }

}

I've searched through similar questions and have understood a small amount about what is causing this error but I have yet to find a solution.

Could someone please help me?

I am using Xcode8 with the latest version of Swift.

  • 2
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Dávid Pásztor Jun 30 '17 at 16:37
  • You have **not** *wrapped up all my code*. `textFieldInt!` can crash. – vadian Jun 30 '17 at 16:38
  • `textFieldInt!` is an optional that you're force unwrapping it. – Mo Abdul-Hameed Jun 30 '17 at 16:38
  • @DávidPásztor I've look at that question and still can't find a solution to my problem. Not sure what you're trying to insinuate here but I'm not sure what's wrong with asking a question that I can't find answered. – James Ridley Jun 30 '17 at 16:39
  • Either your outlets are not connected or your text cannot be parsed into an `Int`. – Sulthan Jun 30 '17 at 16:41

4 Answers4

1

You are force unwrapping using !. You need to do if let or guard let to unwrap because textFieldInt that you force wrapped may be nil.

You should use this code

if let textFieldText = textField.text {
    if let textFieldInt = Int(textFieldText ){
        let convert = textFieldInt * 7
        label4.text = "\(convert)"
    } else{
        print("textFieldInt  is nil")
    }
}else{
    print("text field's text is nil, not too likely")
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Fangming
  • 24,551
  • 6
  • 100
  • 90
  • You mean like `if let textFieldInt: Int? = Int(textField.text!)` ? – James Ridley Jun 30 '17 at 16:38
  • @JamesRidley Updated. – Fangming Jun 30 '17 at 16:38
  • @FangmingNing your inner if statement is not correct. Why do you optional cast an optional int to an int and hence make the return value of your if let optional? You should omit that `...as? Int` part. Optional casting `textField.text` to `String` also makes no sense, since it already returns an optional string. – Dávid Pásztor Jun 30 '17 at 16:43
  • @DávidPásztor Thank you for the edit, there are some typos – Fangming Jun 30 '17 at 16:45
0

The crash occurs due to textFieldInt is nil

the problem is that textField.text is not number-like String.

 let textFieldInt: Int? = Int(textField.text!)

That's why textFieldInt is nil here:

        let convert = textFieldInt! * 7

to solve this issue I would suggest to use this:

if let val = textField.text, let intVal = Int(val) {
    let convert = intVal * 7
    label4.text = String(convert)
}  else {
    label4.text = "Please enter a number!"
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

You declared textFieldInt as an optional Int? Thus, it's possible for it to be nil. The following will assure it's an Int

        guard let textFieldInt = textField.text as? Int else { 
            //handle fail case
            return 
        }
laaksom
  • 2,050
  • 2
  • 18
  • 17
-2

Try this:

@IBOutlet var textField: UITextField!

@IBOutlet var label4: UILabel!

@IBAction func buttonTapped(_ sender: Any) {

  if textField.text != nil {
      if let textFieldInt: Int? = Int(textField.text!){
       let convert = textFieldInt! * 7
       label4.text = String(convert)
     }

  }

  else {
    label4.text = "Please enter a number!"
  }

}
Alfredo Luco G
  • 876
  • 7
  • 18