为什么在我打开localNotification中的string数组时返回nil?

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 =上一行代码后的零!

  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/

我认为你的string“2016-05-26 16:27:17 +0000”需要是“2016-05-26T16:27:17 + 0000”,你的格式需要是“yyyy-MM-dd'T” HH:mm:ssZ“,希望对你有帮助

您在date格式化程序中指定的date格式是错误的。它与您的notificationTime数组中的date不匹配。 你可以试试这个格式:yyyy-MM-dd HH:mm:ssZ作为你的date格式。