2016-05-14 87 views
0

我試圖觸發通知在數據庫中的具體保存時間,並在特定的一天星期五如下所示,但每次我嘗試保存新的提醒它告訴我unexpectedly found nil while unwrapping an Optional value和我確信它包含數據,即使我試圖處理零,但仍然是應用程序崩潰,它不是零,但是當我嘗試在沒有合併日期的情況下觸發通知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

+0

*「我'確定它包含數據,它不是零「*:你爲什麼確定?你有什麼證據?錯誤信息告訴你完全相反。 –

+2

嘗試在不使用'!'的情況下重寫這兩個函數。相反,使用「如果讓」和「守衛讓」。 –

+0

好了之後,我再次打開該應用程序提醒表重新加載提醒我試圖保存,當我打開提醒視圖加載就好,我保存的數據,正如我所提到的,當我更換'localNotification.fireDate = combineDate (提醒。時間!)'與'localNotification.fireDate = reminders.time!''沒有問題發生 – User

回答

1

仍然造成這一切的質量未知的,但是我解決它通過檢查提醒管理對象不等於零然後嘗試火災通知的原因

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) 




    }