NSDateFormatter.dateFromString 在 iOS9 升级后返回 null
NSDateFormatter.dateFromString is returning null after iOS9 upgrade
我的一个应用程序在刚升级到 iOS9 的设备上 运行ning 后出现问题(应用程序的目标是 8.1)。
该应用程序在 iOS9 之前运行良好,但是,仅在设备上升级到 iOS9 后,出现以下问题。
下方名为 'anObsDate' 的变量现在返回 null。我不知道为什么。主机设备上 iOS9 的独立升级似乎触发了此问题的发生。
NSDate *anObsDate = [[self bomDateFormatter] dateFromString:[anObs timeStamp]];
当我 运行 使用以下调试的应用程序时,我们有这个输出:
NSLog(@"Timestamp %@", [anObs timeStamp]);
NSLog(@"Formatter %@", [self bomDateFormatter]);
NSDate *anObsDate = [[self bomDateFormatter] dateFromString:[anObs timeStamp]];
Timestamp 2015-09-17 00:01:10
Formatter <NSDateFormatter: 0x7fe7abc653c0>
我将我的日期格式化程序设置如下:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"];
NSDateFormatter *countDownFormatter = [[NSDateFormatter alloc] init];
[countDownFormatter setLocale:locale];
[countDownFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
[countDownFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
return countDownFormatter;
再次说明这一点,如果我 运行 我的应用程序在 iOS9 之前的设备上,一切正常,并且 dateFromString returns 一个 NSDate。升级后 iOS9 dateFromString returns null.
有什么建议吗?
谢谢。
似乎 NSDateFormatter
在 iOS 9 中的行为发生了变化,因此日期格式中的单引号字符必须与字符串中的字符完全匹配。以前它会接受任何字符(搜索文档,但找不到任何提及此更改的内容)。
时间戳字符串缺少日期格式中定义的 "T"。尝试将您的日期格式更改为:
// Note the "T" has been removed
[countDownFormatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss"];
您的另一个选择是将 "T" 添加到时间戳字符串中:
// Note the "T" has been added
2015-09-17T00:01:10
这应该向后兼容以前版本的iOS。
这解决了我的问题。你也可以用它。
在Objective C中:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
在Swift中:
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
我的一个应用程序在刚升级到 iOS9 的设备上 运行ning 后出现问题(应用程序的目标是 8.1)。
该应用程序在 iOS9 之前运行良好,但是,仅在设备上升级到 iOS9 后,出现以下问题。
下方名为 'anObsDate' 的变量现在返回 null。我不知道为什么。主机设备上 iOS9 的独立升级似乎触发了此问题的发生。
NSDate *anObsDate = [[self bomDateFormatter] dateFromString:[anObs timeStamp]];
当我 运行 使用以下调试的应用程序时,我们有这个输出:
NSLog(@"Timestamp %@", [anObs timeStamp]);
NSLog(@"Formatter %@", [self bomDateFormatter]);
NSDate *anObsDate = [[self bomDateFormatter] dateFromString:[anObs timeStamp]];
Timestamp 2015-09-17 00:01:10
Formatter <NSDateFormatter: 0x7fe7abc653c0>
我将我的日期格式化程序设置如下:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"];
NSDateFormatter *countDownFormatter = [[NSDateFormatter alloc] init];
[countDownFormatter setLocale:locale];
[countDownFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
[countDownFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
return countDownFormatter;
再次说明这一点,如果我 运行 我的应用程序在 iOS9 之前的设备上,一切正常,并且 dateFromString returns 一个 NSDate。升级后 iOS9 dateFromString returns null.
有什么建议吗?
谢谢。
似乎 NSDateFormatter
在 iOS 9 中的行为发生了变化,因此日期格式中的单引号字符必须与字符串中的字符完全匹配。以前它会接受任何字符(搜索文档,但找不到任何提及此更改的内容)。
时间戳字符串缺少日期格式中定义的 "T"。尝试将您的日期格式更改为:
// Note the "T" has been removed
[countDownFormatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss"];
您的另一个选择是将 "T" 添加到时间戳字符串中:
// Note the "T" has been added
2015-09-17T00:01:10
这应该向后兼容以前版本的iOS。
这解决了我的问题。你也可以用它。
在Objective C中:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
在Swift中:
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")