0

I'm trying to trigger notification on specific saved time in database and on specific day which is Friday as shown below, but every time I try to save new reminder it tells me unexpectedly found nil while unwrapping an Optional value and I'm sure it contains data and it's not nil even though I tried to handle nil, still the app crash, however when I try to fire notification without combining dates like this localNotification.fireDate = reminders.time! the app is not crashed and it works just fine.

I guess the problem in combineDate function, how I can solve this problem?

   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

User
  • 123
  • 4
  • 13
  • *"I'm sure it contains data and it's not nil"*: Why are you sure? What's your evidence? The error message is telling you the exact opposite. – Aaron Brager May 14 '16 at 00:43
  • 2
    Try rewriting both functions without using the `!` anywhere. Instead use `if let` and `guard let`. – Aaron Brager May 14 '16 at 00:44
  • well after i open the app again the reminder table is reloaded with the reminder i tried to saved and when I open that reminder the view loads just fine with data that I saved, and as I mentioned when i replace `localNotification.fireDate = combineDate(reminders.time!)` with `localNotification.fireDate = reminders.time!` no problem happens – User May 14 '16 at 00:48
  • 1
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jtbandes May 14 '16 at 01:45

1 Answers1

1

still the reason of causing all this mass unknown however I solved it by checking if reminder managed object is not equal to nil then try to fire notification

 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)




    }
User
  • 123
  • 4
  • 13