当newValue没有分配给它时,为什么这个快速代码有效?

时间:2020-12-10 21:10:41
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)"
    }
}

}

This is the part of code used in the latest Standford IOS 8 course using swift to build a calculator(Youtube address: https://www.youtube.com/watch?v=QLJtT7eSykg)

这是使用swift构建计算器的最新Standford IOS 8课程中使用的代码部分(Youtube地址:https://www.youtube.com/watch?v = QRJtT7eSykg)

Every time I call enter() (press enter), a new number is supposed to be saved in a stack. For example: "8, enter()" --> {8}, "16, enter()" --> {8,16}.

每当我调用enter()(按回车键)时,新的数字应该保存在堆栈中。例如:“8,输入()” - > {8},“16,输入()” - > {8,16}。

I got confused about the computed property "displayValue" here. There is nothing assigned to the "newValue". If there is something like "displayValue = 8", then I know "newValue" is 8, and it all makes sense. But there is not such thing.

我对这里的计算属性“displayValue”感到困惑。没有任何内容分配给“newValue”。如果有类似“displayValue = 8”的东西,那么我知道“newValue”是8,这一切都是有道理的。但是没有这样的事情。

How come it still works?

怎么还能运作?

(What I mean is not the name "newValue" itself,I know it is a default setting by Swift, instead, the missing of assigned value is the one that confuses me)

(我的意思不是名称“newValue”本身,我知道它是Swift的默认设置,相反,缺少指定值是让我困惑的那个)

1 个解决方案

#1


0  

"newValue" is an implicitly defined variable in swift. What he does is a very neat way of letting the label show the values of the double "displayValue" Every time displayValue is changed, the label is automatically updated with the newest (double) value. Or when you write: displayValue = 45.0, the label will also show this value. Very handy when you constantly need to update textfield or labels with data you get from databases, rest interfaces, etc. What "newValue" does is taking the last "setter" value holding that.

“newValue”是swift中隐式定义的变量。他所做的是让标签显示双“displayValue”值的非常简洁的方法每次更改displayValue时,标签会自动更新为最新(double)值。或者当你写:displayValue = 45.0时,标签也会显示这个值。当您经常需要使用从数据库,休息接口等获得的数据更新文本字段或标签时非常方便。“newValue”所做的是将最后一个“setter”值保留为该值。

#1


0  

"newValue" is an implicitly defined variable in swift. What he does is a very neat way of letting the label show the values of the double "displayValue" Every time displayValue is changed, the label is automatically updated with the newest (double) value. Or when you write: displayValue = 45.0, the label will also show this value. Very handy when you constantly need to update textfield or labels with data you get from databases, rest interfaces, etc. What "newValue" does is taking the last "setter" value holding that.

“newValue”是swift中隐式定义的变量。他所做的是让标签显示双“displayValue”值的非常简洁的方法每次更改displayValue时,标签会自动更新为最新(double)值。或者当你写:displayValue = 45.0时,标签也会显示这个值。当您经常需要使用从数据库,休息接口等获得的数据更新文本字段或标签时非常方便。“newValue”所做的是将最后一个“setter”值保留为该值。