gpt4 book ai didi

ios - 当 newValue 没有分配任何内容时,为什么这段快速代码可以工作?

转载 作者:行者123 更新时间:2023-11-30 14:14:36 26 4
gpt4 key购买 nike

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类(class)中使用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 的默认设置,相反,分配值的缺失让我感到困惑)

最佳答案

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

关于ios - 当 newValue 没有分配任何内容时,为什么这段快速代码可以工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31350035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com