import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var inMid = false
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if inMid{
display.text = display.text! + digit
}else{
display.text = digit
inMid = true
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
inMid = false
operandStack.append(displayValue)
println("operandStack = \(operandStack)")
}
var displayValue:Double{
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set{
display.text = "\(newValue)"
}
}
}
这是最新的Standford IOS 8课程中使用swift构建计算器的部分代码(Youtube地址:https ://www.youtube.com/watch?v=QLJtT7eSykg )
每次我调用 enter()(按 enter)时,都应该将一个新数字保存在堆栈中。例如:"8, enter()" --> {8}, "16, enter()" --> {8,16}。
我对这里的计算属性“displayValue”感到困惑。没有分配给“newValue”的任何内容。如果有类似“displayValue = 8”的东西,那么我知道“newValue”是 8,这一切都说得通。但是没有这样的事情。
怎么还能用?
(我的意思不是名称“newValue”本身,我知道它是 Swift 的默认设置,相反,分配值的缺失让我感到困惑)