0

I am trying to add a label to my calculator where it shows the tip amount but I keep getting

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I just want it to display the tip amount as well. I copied it exact for the other UILabel.


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var PriceTxt: UITextField!
    @IBOutlet weak var Tip: UITextField!
    @IBOutlet weak var totalFinal: UILabel!
    @IBOutlet weak var TipAmount: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        totalFinal.text = ""
        TipAmount.text = ""
    }

    @IBAction func Calcualte(_ sender: Any) {
        if  PriceTxt.text! == "" || Tip.text! == "" 
        {
            totalFinal.text = "Input the Numbers"
            TipAmount.text = ""
        }

        else {
        let price = Double(PriceTxt.text!)!
        let tipPer = Double(Tip.text!)!
        let TipMult =  price * (tipPer/100)
        let TipFinal = Double((round(100*TipMult)/100) + price)
        totalFinal.text = "$\(TipFinal)"
        TipAmount.text = "$\(TipMult)"
    }

}
}
Jay Ahmed
  • 9
  • 2
  • Presumably either `PriceTxt.text` or `Tip.text` is nil (or has some value that can't be converted to Double). Don't force unwrap something unless you're *absolutely 100% certain* it has a valid value. – John Montgomery May 28 '20 at 19:38
  • Does this answer your question? [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) – John Montgomery May 28 '20 at 19:41

2 Answers2

0

Try using ? instead of !

! force unwraps and gives you a fatal error if there is no value (nil). ? only unwraps if a value is present.

Also, why unwrap the text here at all?

Use PriceTxt.text == "" || Tip.text == "" without the ! or ?.

  • Also, if you can provide what line the error occurred on exactly, it would be easier to pinpoint the error and provide a code solution. –  May 28 '20 at 19:44
  • I tried add ? and the app doesn't input the TipAmount. It gets rid of the error however. – Jay Ahmed May 28 '20 at 19:45
  • Try commenting out ```TipAmount.text = ""``` in your initial ```if``` statement. It may be reseting the TipAmount value to = "". Your problem is, wherever your error is occuring tipAmount == nil, as you found out from the fatal error. –  May 28 '20 at 19:50
  • The reason for `TipAmount.text = "" ` is because initialy in the storyboard I put TipAmount and I need to clear that out. If I don't clear it out it just displays the "TipAmount" – Jay Ahmed May 28 '20 at 19:56
  • You have ```TipAmount.text = ""``` in two separate places, the first one under ```viewDidLoad``` should reset the text like you want (keep that one). I am saying to comment out the one in the ```if``` statement. –  May 28 '20 at 20:03
  • Also, why unwrap the text here? ```if PriceTxt.text! == "" || Tip.text! == ""``` you can achieve the same thing without any unwrapping. (```if PriceTxt.text == "" || Tip.text! == ""```) –  May 28 '20 at 20:06
  • I commented the one in the if statement, but however it "TipAmount" is still not being cleared out – Jay Ahmed May 28 '20 at 20:10
  • You can try starting with a blank label in the storyboard, and setting it to the inital text you want them to see right after ```viewDidLoad``` maybe? This is too far out of scope from the original question/code sorry ~ work on it for some time and if no luck post the new question/code on stackOverflow. If I helped you with the Fatal Error / force unwrap problem please accept my answer, good luck! –  May 28 '20 at 20:30
0

It will show you the problem in your code.. please always try to avoid force cast

class ViewController: UIViewController {

    @IBOutlet weak var PriceTxt: UITextField!
    @IBOutlet weak var Tip: UITextField!
    @IBOutlet weak var totalFinal: UILabel!
    @IBOutlet weak var TipAmount: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        totalFinal.text = ""
        TipAmount.text = ""
    }

    @IBAction func Calcualte(_ sender: Any) {
        if let getPrice = PriceTxt.text , let getTip = Tip.text
        {
            if getPrice.isEmpty || getTip.isEmpty {
                totalFinal.text = "Input the Numbers"
                TipAmount.text = ""
            }
            else {

                let price = Double(getPrice)!
                let tipPer = Double(getTip)!
                let TipMult =  price * (tipPer/100)
                let TipFinal = Double((round(100*TipMult)/100) + price)
                totalFinal.text = "$\(TipFinal)"
                TipAmount.text = "$\(TipMult)"
            }
        } else {
            print("either PriceTxt or Tip is nil")
        }
    }
}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49