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"
Any idea how to solve it?