我试图在数据库中的特定保存时间和特定日期(星期五)触发通知,如下所示,但每次我尝试保存新提醒时,它都会告诉我在展开可选值时意外发现 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)
}
最佳答案
导致所有这些质量的原因仍然未知,但是我通过检查提醒托管对象是否不等于 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/