I'm following the Stanford classes about iOS 8 development. I have some questions about my code. There is my whole code.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var userIsEnterADigit = false
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsEnterADigit{
display.text = display.text! + digit
}else{
display.text = digit;
userIsEnterADigit = true
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsEnterADigit = false
operandStack.append(displayValue)
print("operandStack = \(operandStack)")
}
var displayValue :Double {
get{
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set{
display.text = "\(newValue)"
userIsEnterADigit = false
}
}
}
I don't know why xcode point
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
having the error:
unexpectedly found nil while unwrapping an Optional value
I check the code with video and don't find the reason. Can someone tell me why?
And help me to understand the newValue
in display.text = "\(newValue)"
. (Why it can appear like this? Is this a grammar of swift?)