Reputation: 23
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
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?
nil
. In this specific case, the @IBOutlet weak var display: UILabel!
outlet might not be connected.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