Please tell me what the problem is error: unexpectedly found nil while unwrapping an Optional value when you click on the calculator button hangs
The error in this line is numberOnScreen = Double (label.text!)!

import UIKit class ViewController: UIViewController {

var numberOnScreen:Double = 0 @IBOutlet weak var label: UILabel! @IBAction func numbers(_ sender: UIButton) { label.text = label.text! + String(sender.tag-1) numberOnScreen = Double(label.text!)! } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. 

    1 answer 1

    Most likely label.text == nil when you access it for the first time. Because of this, an error occurs.

    You can try the following:

     if let _text = label.text { label.text = _text + String(sender.tag-1) if let number = Double(_text){ numberOnScreen = number } } 
    • did not help the same mistake - Michael
    • most likely the value of _text cannot be converted to double. Try if let number = Double (_text) {numberOnScreen = number} - 0rt
    • Why use the tag? What is the title of your button? - Victor Mishustin