Why does it return nil when I unwrap the array of strings in localNotification?

北城余情 提交于 2020-01-05 05:19:21

问题


notificationTime = ["2016-05-26 16:27:17 +0000","2016-05-24 13:29:37 +0000"]
var num = 0
func locaNotification(num:Int)
{
    let dateFormatter = NSDateFormatter()
    dateFormatter.locale = NSLocale.currentLocale()
    dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle

    var dateAsString =  notificationTime[num]
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    var newDate = dateFormatter.dateFromString(dateAsString)!

It returns nil when it is unwrapped here, newDate = nil after the previous line of code!

    var arr = UIApplication.sharedApplication().scheduledLocalNotifications
    for localN:UILocalNotification in arr!
    {
        var notificationFireDate:NSDate = localN.fireDate!
        if notificationFireDate == newDate
        {
            UIApplication.sharedApplication().cancelLocalNotification(localN)
        }
    }
}

回答1:


Your problem is that "yyyy-MM-dd HH:mm" is not right you can see this website http://nsdateformatter.com/

I think that your string "2016-05-26 16:27:17 +0000" need to be "2016-05-26T16:27:17+0000" and your format need to be "yyyy-MM-dd'T'HH:mm:ssZ", I hope this help you




回答2:


the date format you are specifying in your date formatter is wrong.It does not match with the dates in your notificationTime array. You can try this format : yyyy-MM-dd HH:mm:ssZ as your date format.



来源:https://stackoverflow.com/questions/37466837/why-does-it-return-nil-when-i-unwrap-the-array-of-strings-in-localnotification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!