0

Why am I getting "Unexpectedly found nil while unwrapping an Optional value? I check the value of timerSeconds and it is correctly assigned to what I want it to be assigned to. However, when I call the function StartTimer my app is crashing.

300 EggTimer/ViewController.swift:30: Fatal error: Unexpectedly found nil while unwrapping an Optional value 2021-06-02 19:17:04.380375+1000 EggTimer[27674:932041] EggTimer/ViewController.swift:30: Fatal error: Unexpectedly found nil while unwrapping an Optional value (lldb)

import UIKit

class ViewController: UIViewController {
    
let eggTimes : [String : Int] = ["Soft": 300, "Medium": 420, "Hard": 720]
var secondsRemaining: Int?
@IBAction func hardnessSelected(_ sender: UIButton) {
    let hardness = sender.currentTitle!
    let timerSeconds = eggTimes[hardness]!

    print(timerSeconds)
    //until here the code seems to work fine
    
    
    startTimer(secondsRemaining: timerSeconds)
    //call the function start timer and give the secondRemaining argument the value of timerSeconds
    
}
func startTimer (secondsRemaining: Int?){
//create a function called startTimer which accepts an interger as argument called secondsremaining
    Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
        if self.secondsRemaining! > 0 {
            //if the secondsRemaining >
            print ("\(self.secondsRemaining ?? 0) seconds")
            self.secondsRemaining! -= 1
        }else {
            Timer.invalidate()
          }
        }
     
    }

}

djumanji
  • 43
  • 5

2 Answers2

2

Note that in startTimer, self.secondsRemaining does not refer to the same thing as the parameter secondsRemaining:

var secondsRemaining: Int? // self.secondsRemaining

@IBAction func hardnessSelected(_ sender: UIButton) {
   ...
}
func startTimer (secondsRemaining: Int?){ // you never use this parameter
//create a function called startTimer which accepts an interger as argument called secondsremaining
    Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in

        // here you are referring to the var declared outside of the methods
        // which you never assign anything to.
        // this does not refer to the parameter
        if self.secondsRemaining! > 0 {

A simple fix would be to set self.secondsRemaining to the parameter secondsRemaining at the start of startTimer:

func startTimer (secondsRemaining: Int?){ // you never use this parameter
    self.secondsRemaining = secondsRemaining
    Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
        // same as before...
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you! That fixed my code. Could you please explain why can I just not use the secondsRemaining inside the function without creating the variable outside of the func? When I try to do that complier returns an error: secondsRemaining is constant and is not mutable and I should use self.SecondsRemaining – djumanji Jun 05 '21 at 03:53
  • @djumanji You just answered your own question - because `secondsRemaining` is constant. – Sweeper Jun 05 '21 at 03:55
0

Hi @dumanji you also force unwrapped two constants using !. This can crash if you try to use a value that is nil. Especially if you plan on pushing this app to production, this can lead to unintended runtime errors and app crashes.

Examples:

  • let hardness = sender.currentTitle!
  • let timerSeconds = eggTimes[hardness]!

Consider using ?? (the nil coalescing operator) to the right of the constant to provide a default value in case the optional returns nil.

Possible approach:

  • let timerSeconds = eggTimes[hardness] ?? 420

Let me know if this helps.

sivx76
  • 51
  • 3
  • Yes, but maybe they don't want a default value. Also maybe it's better to understand why it's nil and then fix it, rather than just swipe the issue under the rug. ;) – Eric Aya Jun 05 '21 at 12:30