swift - 尝试合并两个日期会导致应用程序崩溃

标签 swift nsdate uilocalnotification

我试图在数据库中的特定保存时间和特定日期(星期五)触发通知,如下所示,但每次我尝试保存新提醒时,它都会告诉我在展开可选值时意外发现 nil 我确信它包含数据并且它不是零,即使我尝试处理零,应用程序仍然崩溃,但是当我尝试在不组合日期的情况下触发通知时,如 localNotification.fireDate = Remembers.时间!应用程序没有崩溃并且工作得很好。

我猜问题出在combineDate函数中,我该如何解决这个问题?

   func combineDate(time: NSDate?) -> NSDate{

            let gregorianCalendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!

            let time: NSDateComponents = gregorianCalendar.components([.Hour, .Minute, .Second], fromDate: time!)
            time.timeZone = NSTimeZone.systemTimeZone()
            let timeCombiner: NSDateComponents = NSDateComponents()
            timeCombiner.timeZone = NSTimeZone.systemTimeZone()
            timeCombiner.hour = time.hour
            timeCombiner.minute = time.minute
            timeCombiner.second = 00
             timeCombiner.weekday = 6
            let combinedDate: NSDate = gregorianCalendar.dateFromComponents(components3)!

            return combinedDate

    }


    func createLocalNotification(){

        let localNotification = UILocalNotification()

        localNotification.timeZone = NSTimeZone.systemTimeZone()
     localNotification.fireDate = combineDate(reminders.time!)

 localNotification.alertBody = reminders.name!

        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)




    }

enter image description here

最佳答案

导致所有这些质量的原因仍然未知,但是我通过检查提醒托管对象是否不等于 nil 然后尝试触发通知来解决它

 func createLocalNotification(){

        let localNotification = UILocalNotification()

        localNotification.timeZone = NSTimeZone.systemTimeZone()
if reminder != nil{
     localNotification.fireDate = combineDate(reminders.time!)
}

 localNotification.alertBody = reminders.name!

        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)




    }

关于swift - 尝试合并两个日期会导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37221207/

相关文章:

ios - 在 Swift 中删除分组表中的行时崩溃

ios - ( ios ) 如何从委托(delegate)中接收 didReceiveLocalNotification

swift - 如何在 Swift 中格式化时间并检查 "working hours"

ios - nextTriggerDate() 不返回 'expected' 值,是否有其他方法来获取重复本地通知的下一个触发日期?

objective-c - 如何播放警报声音并呈现自定义 View 而不仅仅是通知?

ios - 如何创建具有动态数量标签的自定义 UITableViewCell

ios - Scrollview 中的两个动态 Stackview

swift - 晦涩的问题,Google AdMob SDK,XCode 8.2 + Cocoapods 集成

ios - 获取月 ios 中的最后一个星期二

ios - 有没有办法在不包括日期方面的情况下处理一天中的时间?