小编Luk*_*ath的帖子

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);
Run Code Online (Sandbox Code Playgroud)

在失败的设备上,我得到:

PARSING DATE 2010-11-28T20:30:49-0000 AS (null)
Run Code Online (Sandbox Code Playgroud)

但在当地我得到:

PARSING DATE 2010-11-28T20:30:49-0000 AS 2010-11-28 20:30:49 +0000
Run Code Online (Sandbox Code Playgroud)

这让我发疯,我错过了别的什么吗?

我在本地(模拟器)和我的设备(iPhone 4)上运行4.2.发生故障的设备是运行4.2.1的3GS.

任何想法将不胜感激!

iphone objective-c nsdateformatter

12
推荐指数
1
解决办法
1773
查看次数

标签 统计

iphone ×1

nsdateformatter ×1

objective-c ×1