致命错误:在展开可选值时意外发现nil。iOS天气警报应用程序

致命错误:在展开可选值时意外发现nil。iOS天气警报应用程序,ios,swift,Ios,Swift,我正在做一个天气警报应用程序,它可以根据从openweathermap.com获取的天气数据切换警报声音 点击“添加报警”按钮后将触发iAction按钮,如下所示: @IBAction func saveEditAlarm(_ sender: AnyObject) { getWeather() weatherSwitch() let date = Scheduler.correctSecondComponent(date: dateP

我正在做一个天气警报应用程序,它可以根据从openweathermap.com获取的天气数据切换警报声音

点击“添加报警”按钮后将触发iAction按钮,如下所示:

    @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)

    
}
下面是“天气开关”功能,在该功能中,他们将根据天气情况切换天气警报声音

    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"
}



    }

    }
当应用程序运行时,应用程序在self.weatherResult崩溃!错误消息显示“致命错误:在展开可选值时意外发现nil”


知道如何解决吗?

这里的问题是weatherResult,它是可选的,为nil,其强制展开。编译器在构建时无法判断强制展开值是否为nil。但是在运行时,您已经告诉编译器强制展开weatherValue,而不管其值如何。如果那样的话,你就要崩溃了。所以,要解决您的问题,您需要确保为weatherResult赋值。尝试按ctrl+f键并查找weatherResult,然后查看您的值分配到哪里

尝试使用断点进行调试,并通过打印weatherResult打印语句,自己查看它的值。您将看到值为nil。除非您看到值为零,否则将得到崩溃

关于展开零件,有多种方法可以安全地从可选零件获取值

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

这将使您的应用程序免于崩溃,但由于weatherResult为零,因此也不会为该条件设置值。

了解如何安全地打开可选值。此外,除了您之外,没有人知道名为weatherResult的家伙来自何方。这是一个异步问题,
getWeather()
异步工作。您需要一个完成处理程序。这可以避免崩溃,但不能解决实际(异步)问题。