依赖于硬件的NSDateFormatter dateFromString:bug(返回nil) [英] Hardware-dependent NSDateFormatter dateFromString: bug (returns nil)

查看:118
本文介绍了依赖于硬件的NSDateFormatter dateFromString:bug(返回nil)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个日期字符串需要转换为NSDates。
我的解析代码如下:

I have several date strings that I need to convert to 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];

上述代码在我们测试的所有设备和模拟器中都能正常工作。
奇怪的是,当上述代码在运行iOS 6.1或7.0.x的任何iPhone 5上运行时,行 self.date = [f dateFromString:s]; 每次都返回nil。

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]; returns nil every time.

我检查过并且字符串 s 存在并且在比较时包含相同的字符与正确解析日期的设备并排。

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.

这是一个示例日期字符串:
`2013-10-31T21:50: 00-06:00'

This is an example date string: `2013-10-31T21:50:00-06:00'

我在这里遗漏了什么吗?

Am I missing something here?

推荐答案

这是因为您没有将日期格式化程序的语言环境设置为特殊的 en_US_POSIX 语言环境。很可能你的iPhone 5在24小时设置中有不同的设置。

It's because you are not setting the date formatter's locale to the special en_US_POSIX locale. Most likely your iPhone 5 has a different setting for the 24-hour setting.

每当你解析一个固定格式的字符串时,你需要设置特殊的语言环境。

You need to set the special locale whenever you parse a fixed format string.

这篇关于依赖于硬件的NSDateFormatter dateFromString:bug(返回nil)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆