-2

I am creating a simple guessing game on Xcode where the user has to correctly guess the number (between 0 and 5). I thought that I had checked for a nil value for my textFieldIntto ensure the app does not crash if no value is submitted. Unfortunately this does not seem to be the case and I am not sure where my fault lies.

If I do not enter a value and press the button, I receive the following error message:

fatal error: unexpectedly found nil while unwrapping an Optional value

I have had this issue before and managed to solve it by correctly checking the right optional for nil. Though now it seems to still have 'unexpectedly found nil' even though a nil value has been accounted for.

@IBOutlet var textField: UITextField!

@IBOutlet var label: UILabel!

@IBAction func buttonTapped(_ sender: Any) {
    let textFieldInt: UInt32 = UInt32(textField.text!)!
    let number = arc4random_uniform(6)

    if textFieldInt != nil {
        if textFieldInt != number {
            label.text = "Wrong! It was \(number)"
        }
        else if textFieldInt == number {
            label.text = "You're right!"
        }

    else {
        label.text = "Please enter a number!"
    }
}
}
  • Your `textFieldInt` is not an optional, and `if textFieldInt != nil` should show a compiler warning "comparing non-optional value of type 'UInt32' to nil always returns true" – So you have two forced unwraps which do *not* check for nil. – Martin R Jul 05 '17 at 12:41

2 Answers2

0

Use if let or guard let to test your values. For example

if let textFieldInt = UInt32(textField.text!)  {
    //textField.text! can be converted to UInt32
    if textFieldInt != number {
        label.text = "Wrong! It was \(number)"
    }
    else if textFieldInt == number {
        label.text = "You're right!"
    }
}else {
    //textField.text! can NOT be converted to UInt32
    label.text = "Please enter a number!"
}

Or

guard let textFieldInt = UInt32(textField.text!) else {
    //textField.text! can NOT be converted to UInt32
    label.text = "Please enter a number!"
    return
}
//textField.text! can be converted to UInt32
if textFieldInt != number {
    label.text = "Wrong! It was \(number)"
}
else if textFieldInt == number {
    label.text = "You're right!"
}
Fangming
  • 24,551
  • 6
  • 100
  • 90
0

I believe the error occurs at

let textFieldInt: UInt32 = UInt32(textField.text!)!

If the textField is empty then textField.text will be nil and textField.text! will cause the crash.

You can use like

        @IBAction func buttonTapped(_ sender: Any) {
            if textField.text != nil {
         let textFieldInt: UInt32 = UInt32(textField.text!)!
            let number = arc4random_uniform(6)

            if textFieldInt != nil {
                if textFieldInt != number {
                    label.text = "Wrong! It was \(number)"
                }
                else if textFieldInt == number {
                    label.text = "You're right!"
                }

            else {
                label.text = "Please enter a number!"
            }
        }
      }
              else {
              label.text = "Please enter a number!"
        }
  }
Aravind A R
  • 2,674
  • 1
  • 15
  • 25