NSDateFormatter 在两个不同设备上的行为不一致
我遇到了一些问题,NSDateFormatter 在一个用户的设备上失败(解析字符串时返回 nil),而当我在本地运行它时(在模拟器中或在我的设备上)运行得很好。
我试图排除可能导致这种行为差异的原因。我的第一个想法是区域设置,但我尝试显式设置它以确保始终使用相同的区域设置,但这没有什么区别。
这是代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
[dateFormatter setLocale:locale];
[locale release];
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSLog(@"PARSING DATE %@ AS %@", dateString, theDate);
在发生故障的设备上,我得到:
PARSING DATE 2010-11-28T20:30:49-0000 AS (null)
但在本地我得到:
PARSING DATE 2010-11-28T20:30:49-0000 AS 2010-11-28 20:30:49 +0000
这让我发疯,我是否错过了其他东西?
我在本地(模拟器)和我的设备(iPhone 4)上运行 4.2。发生故障的设备是运行 4.2.1 的 3GS。
任何想法将不胜感激!
I'm having a bit of a problem with NSDateFormatter failing on one user's device (returning nil when parsing a string) and working perfectly when I run it locally (either in the simulator or on my device).
I'm trying to rule out what could be causing a difference in this behaviour. My first thought was the locale but I've tried setting it explicitly to ensure the same locale is always used but it makes no difference.
Here is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
[dateFormatter setLocale:locale];
[locale release];
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSLog(@"PARSING DATE %@ AS %@", dateString, theDate);
On the failing device, I get:
PARSING DATE 2010-11-28T20:30:49-0000 AS (null)
But locally I get:
PARSING DATE 2010-11-28T20:30:49-0000 AS 2010-11-28 20:30:49 +0000
This is driving me crazy, am I missing something else?
I am running 4.2 locally (simulator) and on my device (an iPhone 4). The failing device is a 3GS running 4.2.1.
Any ideas would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很高兴地说我最终解决了这个问题,我必须向 @bendodson 帮助我解决了这个问题。 aBitObvious 在上面的评论中也谈到了这个问题;如果可以的话,我会投票给他。
用户的设备和我的设备之间有一个区别,那就是他的设备设置为使用 12 小时制,而我的设备则没有。这一件事意味着 NSDateFormatter 无法解析上面示例中的时间并返回 nil。
到目前为止,这个问题对我来说最大的问题是无法在本地重现该问题!
所以,要明确的是,要解决这个问题;也就是说,如果您正在解析已知固定格式的日期/时间字符串(通常来自某些 API,就像我的情况一样),您应该为日期格式化程序设置正确的区域设置,通常是 en_US_POSIX。
如需了解更多信息,请阅读 Apple QA1480。
I'm pleased to say that I eventually got to the bottom of this issue and I must pass on my thanks to @bendodson on Twitter for helping me out with this. aBitObvious also hit on the issue in his comment above; I'd have up-voted him if I could.
There was one difference between the user's device and mine, and that was that his device was set to use the 12 hour clock and mine was not. This single thing meant that the NSDateFormatter was unable to parse the time in the above examples and returned nil.
By far the biggest issue for me with this problem was being unable to reproduce the problem locally!
So, to be clear, to solve this issue; that is, if you are parsing date/time strings that are in a known, fixed format (often coming from some API as this was in my case), you should set the correct locale for the date formatter, which will often be en_US_POSIX.
For more information on this, read Apple QA1480.