Search code examples
swiftmacosnstextfield

NSTextField: Converting String to Int aborts (nil) outside of Xcode


I have a MacOS app that has been working fine for around a year. An NSTextField on a screen containing 20 NSTextFields started aborting with "unexpectedly found nil while unwrapping an Optional value". So far, I've done the following:

  • Removed and reassigned the textfield's IBOutlet link
  • Deleted and rebuilt the textfield

This fixes the app when I run in Xcode in my Development account. When I generate an archive and transfer it to my Production account, it still aborts.

I'm going to assume this is still the culprit, since I don't really get much from the core dump (because I probably don't understand what I'm looking at).

Here's my code. (game_number is an Int)

game_number = Int(gameNumberTextField.stringValue)!

When I split the code and do this:

let theNumber = (gameNumberTextField.stringValue)!
game_number = Int(theNumber)

theNumber is a String and correct, but game_number is nil


Solution

  • NSTextField parent class NSControl has a property called integerValue which returns an Int:

    game_number = gameNumberTextField.integerValue