NSDateFormatter在两个不同设备上的行为不一致 [英] Inconsistent behaviour with NSDateFormatter on two different devices

查看:136
本文介绍了NSDateFormatter在两个不同设备上的行为不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,NSDateFormatter在一个用户的设备上失败(在解析字符串时返回nil)并且在本地运行时(在模拟器或我的设备上)完美地工作。

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.

以下是代码:

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)

但我在当地获得:

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?

我在本地(模拟器)和我的设备(iPhone 4)上运行4.2。失败的设备是运行4.2.1的3GS。

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!

推荐答案

我很高兴地说我最终解决了这个问题的根源,我必须转而感谢 @ bendodson 帮助我解决了这个问题。 aBitObvious也在上面的评论中找到了这个问题;如果可以的话,我会投票给他。

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.

用户的设备和我的设备之间存在一个区别,那就是他的设备已经设置为使用12小时钟和我的不是。这一点意味着NSDateFormatter无法解析上面示例中的时间并返回nil。

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!

所以,要明确,要解决这个问题;也就是说,如果您正在解析已知的固定格式的日期/时间字符串(通常来自某些API,就像我的情况那样),您应该为日期格式化程序设置正确的语言环境,通常是en_US_POSIX。

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.

...
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[locale release];

有关详细信息,请阅读 Apple QA1480

For more information on this, read Apple QA1480.

这篇关于NSDateFormatter在两个不同设备上的行为不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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