尝试合并两个日期会导致应用崩溃

时间:2016-05-14 00:36:22

标签: swift nsdate uilocalnotification

我尝试触发数据库中特定保存时间的通知以及星期五特定日期,如下所示,但每次我尝试保存新提醒时,都会告诉我unexpectedly found nil while unwrapping an Optional value和我&#39 ;我确定它包含数据并且它不是零,即使我试图处理nil,仍然是应用程序崩溃,但是当我尝试触发通知而不合并这样的日期localNotification.fireDate = reminders.time!时,应用程序不会崩溃并且它运作得很好。

我猜在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

1 个答案:

答案 0 :(得分:1)

仍然是导致所有这些质量未知的原因但是我通过检查提醒管理对象是否不等于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)




    }