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)!

在这里展开时返回nil,在前一行代码之后newDate=nil!
    var arr = UIApplication.sharedApplication().scheduledLocalNotifications
    for localN:UILocalNotification in arr!
    {
        var notificationFireDate:NSDate = localN.fireDate!
        if notificationFireDate == newDate
        {
            UIApplication.sharedApplication().cancelLocalNotification(localN)
        }
    }
}

最佳答案

你的问题是“yyyy-MM-dd HH:MM”不对,你可以看到这个网站http://nsdateformatter.com/
我认为您的字符串“2016-05-26 16:27:17+0000”需要为“2016-05-26T16:27:17+0000”,格式需要为“yyyy-MM-dd-T'HH:MM:ssZ”,希望对您有所帮助

关于ios - 当我在localNotification中解开字符串数组时,为什么返回nil?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37466837/

10-14 20:09