我试图在iTunes U上按照斯坦福大学的Swift课程,但在创建输入键时我遇到了困难。当我在模拟器中按下回车键时,应用程序崩溃并且没有给出我原因的描述。这是代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var display: UILabel!
var userIsInTheMiddleOfTypingNumber = false
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber == false {
display.text = digit
userIsInTheMiddleOfTypingNumber = true
} else {
display.text = display.text! + digit
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsInTheMiddleOfTypingNumber = false
operandStack.append(displayValue)
print("operandStack = \(operandStack)")
}
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
} set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingNumber = false
}
}
}
答案 0 :(得分:1)