-2

I'm trying to figure out this

unexpectedly found nil while unwrapping an Optional value

Code works fine until I try computeFahreheit Button with an empty UITextField.

Tons of stuff on this subject, I just can't figure it out for this code.

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var inputCelsius: UITextField!
@IBOutlet weak var outputFahrenheit: UILabel!

@IBAction func computeFahreheit(_ sender: Any) {

    var convertedNumber = Int(inputCelsius.text!)!
    convertedNumber = convertedNumber * 9/5 + 32
    outputFahrenheit.text = "\(convertedNumber)F"

    if  (inputCelsius.text == nil) {
        outputFahrenheit.text = "Enter Celsius"
    }else{
        outputFahrenheit.text = "\(convertedNumber)"
    }

Any help would be appreciated, thank you.

Kerberos
  • 4,036
  • 3
  • 36
  • 55
  • 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) – Tamás Sengel Nov 02 '17 at 14:35
  • The answer below is good and does answer your question but presumably you'll have issues if anything that isn't an integer is entered into the textbox too? You should do some validation on the value before you try the conversion. – ThrowingSpoon Nov 02 '17 at 14:37
  • 2
    I call the force-unwrap (`!`) operator the "crash if nil" operator. As a new Swift developer, you should avoid using it until you understand what it does. – Duncan C Nov 02 '17 at 14:48

3 Answers3

1

By writing Int(inputCelsius.text!)! you unwrap text property and the converted numeric value. Moreover, text might be an empty string or non-integer value. In that case you crash when Int("")! is executed). It's also possible inputCelsius itself is nil.

Use guard statement to determine whether a variable has nil value. likewise (Cleaner than 'if statement'):

guard let numberString = inputCelsius.text, let number = Int(inputCelsius.text) else {
    return
}

/* Do whatever you want with the number... */

About guard statement from Apple documentation:

A guard statement, like an if statement, executes statements depending on the Boolean value of an expression. You use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed. Unlike an if statement, a guard statement always has an else clause—the code inside the else clause is executed if the condition is not true.

DanielH
  • 685
  • 5
  • 6
0

Don't try to unwrapped forcibly Int(inputCelsius.text!)!

if var convertedNumber = Int(inputCelsius.text!) {
    convertedNumber = convertedNumber * 9/5 + 32
    outputFahrenheit.text = "\(convertedNumber)F"

    if  (inputCelsius.text == nil) {
        outputFahrenheit.text = "Enter Celsius"
    }else{
        outputFahrenheit.text = "\(convertedNumber)"
    }
}

Note: Try with number pad of keyboard type if you want to convert it integer else you have to do more stuff for checking valid number.

Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34
0

You're unwrapping value here:

inputCelsius.text! 

You should avoid using (!) and rather do:

if let inputText = inputCelsius.text {
var convertedNumber = Int(inputText) 
(...)
}
Mila Meko
  • 22
  • 5
  • This is not optional "chaining", it is "unwrapping"; you're also not unwrapping the optional `Int`. – jscs Nov 02 '17 at 15:55