xcodemacosnsintegerswift5

NSInteger 0+0=4300282608


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)"
    }
}

enter image description here

Upon uploading the image current count ends up being a random number on each run...


Solution

  • I’d suggest you add a breakpoint in addAction and confirm which of those two values is incorrect. On the basis of what you’ve shared with us, presumably currentCount isn’t what you think it is.

    The only minor logical disconnect I can see here is that on viewDidLoad, you are assuming that amountToAdd and currentCount are both 0, though it’s technically possible for whomever presented this view controller might have reset one of those values (possibly incorrectly) between the time that the view controller was instantiated and when it was loaded.

    To resolve this logical disconnect, you can explicitly use the those variables rather than 0:

    override func viewDidLoad() {
        super.viewDidLoad()
        currentCountLabel.integerValue = currentCount
        amountToAddTextField.integerValue = amountToAdd
    }
    

    Or if you want loading of the view controller to reset the view, then have both viewDidLoad and clearAction call some common routine:

    override func viewDidLoad() {
        super.viewDidLoad()
        resetValues()
    }
    
    @IBAction func clearAction(_ sender: Any) {
        resetValues()
    }
    
    private func resetValues() {
        currentCount = 0
        amountToAdd = 0
    
        currentCountLabel.integerValue = 0
        amountToAddTextField.integerValue = 0
    }
    

    If you have some other code that is presenting this view controller, I’d suggest you check that for anything that might have set (possibly incorrectly) currentCount.

    Or put a breakpoint in viewDidLoad and add a watch on the value that isn’t what you think it should be. E.g. I control-clicked on currentCount in the variables view, and chose “Watch currentCount”, and it will now stop every time that value changes.

    enter image description here