简体   繁体   English

NSInteger 0 + 0 = 4300282608

[英]NSInteger 0+0=4300282608

I am developing a simple calculator app to understand how OSX development works (this is not homework). 我正在开发一个简单的计算器应用程序,以了解OSX开发的工作原理(这不是功课)。 When addAction(_ sender: NSButton) is fired it adds the currentCount and amountToAdd together then updates the currentCount and sets that to currentCountLabel . 当触发addAction(_ sender: NSButton) ,它会将currentCountamountToAdd一起添加,然后更新currentCount并将其设置为currentCountLabel

The Issue: When the app first starts currentCount and amountToAdd are set to 0 but when added together equals 4,300,282,608. 问题:当应用程序首次启动时, currentCountamountToAdd设置为0但加在一起时等于4,300,282,608。 If I hit the clear button before doing addition it equals 0 which is correct. 如果我在添加之前按下清除按钮,则等于0,这是正确的。

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... 上传图像后,当前计数最终成为每次运行时的随机数...

I'd suggest you add a breakpoint in addAction and confirm which of those two values is incorrect. 我建议你在addAction添加一个断点,并确认这两个值中的哪一个是不正确的。 On the basis of what you've shared with us, presumably currentCount isn't what you think it is. 根据您与我们分享的内容,推测currentCount不是您认为的那样。

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. 我在这里看到的唯一一个小的逻辑断开是在viewDidLoad ,你假设amountToAddcurrentCount都是0 ,虽然从技术上讲,对于任何人来说,这个视图控制器可能已经重置了这些值之一(可能是错误的)视图控制器已实例化,并在加载时。

To resolve this logical disconnect, you can explicitly use the those variables rather than 0: 要解决此逻辑断开连接,您可以显式使用这些变量而不是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: 或者,如果要加载视图控制器以重置视图,则让viewDidLoadclearAction调用一些常见的例程:

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 . 如果您有其他代码提供此视图控制器,我建议您检查可能已设置(可能不正确) currentCount

Or put a breakpoint in viewDidLoad and add a watch on the value that isn't what you think it should be. 或者在viewDidLoad放置一个断点,并在值上添加一个不是您认为应该的值的监视。 Eg I control -clicked on currentCount in the variables view, and chose “Watch currentCount”, and it will now stop every time that value changes. 例如,我控制 - 在变量视图中单击currentCount ,并选择“Watch currentCount”,现在每次该值更改时它都会停止。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM