0

Can anyone help me understand why the first line of code generates a runtime EXC_BAD_INSTRUCTION error and the second line does not?

let actualHandicap = Float(exactHandicap.text ?? "0.0")
let shotsGiven = Int(numShotsGiven.text ?? "0")

The purpose of these two lines of code are to initialise the float actualhandicap and the int shotsGiven. I want them both to be set to zero should the two UITextField instances (exactHandicap and numShotsGiven be nil.

This code is an excerpt from a segue unwind method I've built based on a tutorial from Apple.

The app compiles and runs but will always crash when the unwind method is called. The stdout displays fatal error: unexpectedly found nil while unwrapping an Optional value

The contents of the local variables in the debugger indicate that actualHandicap has not been set to zero as expected, but shotsGiven has.

As a Swift newbie, I expect I'm missing something fundamental with how I'm using the Int() and Float() functions to cast the strings to zero, but I'm unable to see what's going wrong.

Any help gratefully received.

Dave
  • 71
  • 10
  • 3
    Check `exactHandicap`. Is it `nil`? Perhaps you did not connect the outlet correctly? – Martin R Jan 16 '17 at 21:41
  • exactHandicap and/or numShotsGiven are probably declared as implicitly unwrapped optionals with the `!` operator. That's the norm for outlets, but that means if they contain nil, your code will crash. You should change your expressions to `exactHandicap?.text ?? "0.0"` (add a question mark after the name of the outlet) – Duncan C Jan 16 '17 at 22:12
  • 1
    @DuncanC: Well, an unconnected outlet is a *programming error,* and the crash helps to find and fix that early. – Martin R Jan 16 '17 at 22:19
  • But that answers the OP's question. The `??` resolves the fact that the text property of `UITextField`/`UITextView` is optional, but if the implicitly unwrapped optional outlet is nil, the code crashes. – Duncan C Jan 17 '17 at 00:21
  • Thank you for the prompt responses. I set the code to include the additional '?' but this leads to the same runtime error and same variable contents in the debug window. I'll dig deeper on the comment from @MartinR about the unconnected outlet and see if that's fruitful. – Dave Jan 17 '17 at 15:07
  • So, I'm now sure that the outlet for `exactHandicap` is connected correctly. It's a class property and there is a solid circle next to it's _IBOutlet_ line. Second, I've looked at some example code to safely unwrap the variable in another way. I tried the following `if let actualHandicap = Float((exactHandicap?.text)!) { let actualHandicap = Float(exactHandicap.text!) } else { let actualHandicap = 0 }` and I now get `Use of unresolved identifier _exactHandicap_' in the editor where my first code line compiled OK. Ideas? – Dave Jan 17 '17 at 16:52
  • To be specific, the compiler error happens _after_ the code I included in my previous comment. I reference _exactHandicap_ in a function call – Dave Jan 17 '17 at 19:00
  • I have a solution. The original two lines of code from my first post are still in place but I've tweaked the function call where the run-time crash was happening from `player = Player(firstName: firstName, lastName: secondName, emailAddress: "", exactHandicap: actualHandicap, shotsGiven: shotsGiven!, mugShot: photo)` to `player = Player(firstName: firstName, lastName: secondName, emailAddress: "", exactHandicap: actualHandicap ?? 0, shotsGiven: shotsGiven!, mugShot: photo`. I'm still unsure why the original line doesn't trap the nil value of _exacthandicap_. Any insight welcome. – Dave Jan 17 '17 at 21:24

0 Answers0