0

I am doing a weather alarm app, which can switch the alarm sound based on the fetched weather data from openweathermap.com.

The IBACTION button, in which will be triggered once the "add alarm" button is tapped, is as follows:

    @IBAction func saveEditAlarm(_ sender: AnyObject) {
    
    getWeather()
    
    weatherSwitch()
    
    let date = Scheduler.correctSecondComponent(date: datePicker.date)
    let index = segueInfo.curCellIndex
    var tempAlarm = Alarm()
    tempAlarm.date = date
    tempAlarm.label = segueInfo.label
    tempAlarm.enabled = true
    tempAlarm.mediaLabel = weatherSoundName
    tempAlarm.mediaID = weatherSoundName
    tempAlarm.snoozeEnabled = snoozeEnabled
    tempAlarm.repeatWeekdays = segueInfo.repeatWeekdays
    tempAlarm.uuid = UUID().uuidString
    tempAlarm.onSnooze = false
    if segueInfo.isEditMode {
        alarmModel.alarms[index] = tempAlarm
    }
    else {
        alarmModel.alarms.append(tempAlarm)
    }
    tableView.reloadData()
    
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "newDataNotif"), object: nil)
    
    //MainAlarmViewController.tableView.refresh()
    
    
  //  self.performSegue(withIdentifier: Id.saveSegueIdentifier, sender: self)
    
    
    self.performSegue(withIdentifier: "saveEditSegue", sender: self)

    
}

And below is the "weatherSwitch" function in which they will switch the weather alarm sound according to the weather.

    func getWeather() {
    WeatherManager.shared.getWeather(onSuccess: { (result) in
     
        self.condition = self.weatherResult!.current.weather.description
        
        
        
    }) { (errorMessage) in
        debugPrint(errorMessage)
    }
}







func weatherSwitch() {

    getWeather()
    
    if weatherResult != nil {
switch weatherSoundName {
case _ where weatherResult!.current.weather.description.contains("thunderstorm"):
    weatherSoundName = "yt1s.com - Swallowing DustПыль глотаю"

case _ where weatherResult!.current.weather.description.contains("drizzle"):
    weatherSoundName = "yt1s.com - Swallowing DustПыль глотаю"
case _ where weatherResult!.current.weather.description.contains("rain"):
    weatherSoundName = "yt1s.com - Swallowing DustПыль глотаю"
case _ where weatherResult!.current.weather.description.contains("snow"):
    weatherSoundName = "yt1s.com - Swallowing DustПыль глотаю"
case _ where weatherResult!.current.weather.description.contains("clouds"):
    weatherSoundName = "yt1s.com - Swallowing DustПыль глотаю"
case _ where weatherResult!.current.weather.description.contains("clear"):
    weatherSoundName = "yt1s.com - Swallowing DustПыль глотаю"
default:
    weatherSoundName = "yt1s.com - MGMT  Little Dark Age Video"
}



    }

    }

When the app is run, the app crashed at the self.weatherResult! with the error message says "Fatal error: Unexpectedly found nil while unwrapping an Optional value"

enter image description here

Any idea how to solve it?

1 Answers1

0

The issue here is your weatherResult which is an optional is nil and its force unwrapped .Compiler can't tell whether the force unwrapped value is nil or not in build time. But during the run time you have already told the compiler unwrap forcefully the weatherValue regardless of its value. In that case you get the crash. So to solve your problem you need to make sure you assign value to weatherResult. Try doing ctrl + f and find weatherResult and see where your value is getting assigned.

Try to debug with breakpoints and print statement by printing the weatherResult and see it's value yourself. You'll see the value as nil. You will get the crash until and unless you see the value as nil.

About the unwrapping part, there are multiple ways to safely get the value from an optional.

if let result = weatherResult{
    self.condition = self.result.current.weather.description
}

This will present your app from crashing but since weatherResult is nil, the value also will not be set for the condition.

Saroj Tiwari
  • 125
  • 7