gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:03:36 24 4
gpt4 key购买 nike

我试图在数据库中的特定保存时间和特定日期(星期五)触发通知,如下所示,但每次我尝试保存新提醒时,它都会告诉我在展开可选值时意外发现 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com