How can I unwrap seconds remaining to prevent it from being nil?

89 views Asked by At

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

}

2

There are 2 answers

2
Sweeper On BEST ANSWER

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...
1
sivx76 On

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.