I am developing a simple calculator app to understand how OSX development works (this is not homework). When addAction(_ sender: NSButton)
is fired it adds the currentCount
and amountToAdd
together then updates the currentCount
and sets that to currentCountLabel
.
The Issue: When the app first starts currentCount
and amountToAdd
are set to 0 but when added together equals 4,300,282,608. If I hit the clear button before doing addition it equals 0 which is correct.
The Question: How can I change the code do the correct calculation the first time. It appears to be a casting issue.
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)"
}
}
Upon uploading the image current count ends up being a random number on each run...