为什么当我在 localNotification 中打开字符串数组时 return nil?
Why does it return nil when I unwrap the array of strings in localNotification?
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)!
这里展开时returnsnil,上一行代码后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",希望对你有帮助
您在日期格式化程序中指定的日期格式 wrong.It 与 notificationTime 数组中的日期不匹配。
您可以尝试这种格式:yyyy-MM-dd HH:mm:ssZ 作为您的日期格式。
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)!
这里展开时returnsnil,上一行代码后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",希望对你有帮助
您在日期格式化程序中指定的日期格式 wrong.It 与 notificationTime 数组中的日期不匹配。 您可以尝试这种格式:yyyy-MM-dd HH:mm:ssZ 作为您的日期格式。