anotherStupid
anotherStupid

Reputation: 23

lldb error in swift

So I am trying to follow standford's swift lecture series.. However, when I am trying to play the following code, it get 'lldb' error. Any help will be appreciated.. Many thanks

import UIKit
class ViewController: UIViewController {


@IBOutlet weak var display: UILabel!

var userIsIntheMiddleOfTyping = false

@IBAction func touchDigit(_ sender: UIButton) {
    let digit = sender.currentTitle!

    if userIsIntheMiddleOfTyping {
        let textCurrentInDisplay = display.text!
        display.text = textCurrentInDisplay + digit
    } else {
        display.text = digit
    }
    userIsIntheMiddleOfTyping = true
  }
}

It is to note that when the debugger opens the following line of code is highlighted,

    let digit = sender.currentTitle!

Upvotes: 2

Views: 3802

Answers (1)

Idan
Idan

Reputation: 5450

In the line of code below, you are forcing unwrapping of optional value.

let digit = sender.currentTitle!

The compiler is trying to tell you that.

How to solve the problem?

  1. Make sure that the all the values are connected and are not nil. In this specific case, the @IBOutlet weak var display: UILabel! outlet might not be connected.
  2. If you are not sure that if the value is nil or not, use conditional statements and handle the nil case. For example:

    if let digit = sender.currentTitle {
      print("Great, its working")
    } else {
      print("error: currentTitle is nil")
    }
    

Upvotes: 1

Related Questions