我正在开发一个简单的计算器应用程序来了解 OSX 开发的工作原理(这不是家庭作业)。当addAction(_ sender: NSButton)
被触发时,它将currentCount
andamountToAdd
一起添加,然后更新currentCount
and 将其设置为currentCountLabel
.
问题:当应用程序首次启动currentCount
并amountToAdd
设置为 0 但加在一起时等于 4,300,282,608。如果我在添加之前点击清除按钮,它等于 0,这是正确的。
问题:如何更改代码第一次进行正确的计算。看来是选角问题。
import Cocoa
class ContainerViewController: NSViewController {
var currentCount: Int = 0
var amountToAdd: Int = 0
@IBOutlet weak var currentCountLabel: NSTextField!
@IBOutlet weak var amountToAddTextField: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
currentCountLabel.integerValue = 0
amountToAddTextField.integerValue = 0
}
@IBAction func amountTextField(_ sender: NSTextField) {
amountToAdd = amountToAddTextField.integerValue
}
@IBAction func addAction(_ sender: NSButton) {
currentCount = currentCount + amountToAdd
currentCountLabel.integerValue = currentCount
}
@IBAction func clearAction(_ sender: Any) {
currentCount = 0
amountToAdd = 0
currentCountLabel.integerValue = 0
amountToAddTextField.integerValue = 0
}
func getEntryLog() -> String {
return "\(currentCount) + \(amountToAdd) = \(currentCount + amountToAdd)"
}
}
上传图像后,当前计数最终成为每次运行的随机数......