iosswiftdictionaryswift-optionals

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


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

}


Solution

  • 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...