[英]Hardware-dependent NSDateFormatter dateFromString: bug (returns nil)
I have several date strings that I need to convert to NSDates.我有几个日期字符串需要转换为 NSDates。 My parsing code is the following:
我的解析代码如下:
NSString *s = [pair objectForKey:@"nodeContent"];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZ"];
self.date = [f dateFromString:s];
The above code works fine in all the devices and simulators we've tested.上面的代码在我们测试过的所有设备和模拟器中都可以正常工作。 The strange thing is that when the above code runs on any iPhone 5 running iOS 6.1 or 7.0.x , the line
self.date = [f dateFromString:s];
奇怪的是,当上面的代码在任何运行 iOS 6.1 或 7.0.x 的 iPhone 5 上运行时,这行
self.date = [f dateFromString:s];
returns nil every time.每次都返回零。
I have checked and the string s
exists and contains the same characters when compared side-by-side with a device that does parse the date correctly.我已经检查过,当与正确解析日期的设备并排比较时,字符串
s
存在并且包含相同的字符。
This is an example date string: `2013-10-31T21:50:00-06:00'这是一个示例日期字符串:`2013-10-31T21:50:00-06:00'
Am I missing something here?我在这里错过了什么吗?
It's because you are not setting the date formatter's locale to the special en_US_POSIX
locale.这是因为您没有将日期格式化程序的语言环境设置为特殊的
en_US_POSIX
语言环境。 Most likely your iPhone 5 has a different setting for the 24-hour setting.很可能您的 iPhone 5 具有不同的 24 小时设置。
You need to set the special locale whenever you parse a fixed format string.每当您解析固定格式字符串时,您都需要设置特殊的语言环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.