2
我的事件日期输出正确,我的页面没有错误,但是我的应用崩溃了,请帮忙?如何为UILocalNotifiication设置NSDate
我认为它可能有一些与我的NSString日期转换为NSDate的
NSLog("This is the EventDate: \(eventdate)")
let date = eventdate as? NSString
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
var DateInFormat = dateFormatter.stringFromDate((date as? NSDate)!)
var notification:UILocalNotification = UILocalNotification()
notification.alertBody = "\(eventtitle) is about to start at the \(rsPubName)"
notification.category = "FIRST_CATEGORY"
notification.alertAction = "heyaaa"
notification.fireDate = date as? NSDate!
NSLog("This is the date: \(date)")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
有很多奇怪的可选事情正在进行。例如,'(date as?NSDate)!'非常有趣。为什么不使用'date as! NSDate'?此外,字符串不能简单地铸造日期。他们必须通过'NSDateFormatter'的'dateFromString:'方法。 – tktsubota