NSDateFormatter 在 OS 4.0 中返回 nil

发布于 2024-09-07 06:55:55 字数 456 浏览 1 评论 0 原文

我在 OS 3.x 上运行了以下代码

NSString *stringDate = @"2010-06-21T20:06:36+00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *theDate = [dateFormatter dateFromString:stringDate];
NSLog(@"%@",[dateFormatter stringFromDate:theDate]);

,但现在在 iOS4 模拟器下的最新 xcode 3.2.3 中,变量 theDate 为零。

我查看了类参考,没有发现任何已弃用或针对 iOS4 使用这些特定方法进行不同实现的内容。我遗漏了什么?

I had the following code working on on OS 3.x

NSString *stringDate = @"2010-06-21T20:06:36+00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *theDate = [dateFormatter dateFromString:stringDate];
NSLog(@"%@",[dateFormatter stringFromDate:theDate]);

but now in the newest xcode 3.2.3 under the iOS4 simulator, the varialble theDate is nil.

I have looked through the class reference and do not see anything deprecated or implemented differently for iOS4 with these specific methods. What did i leave out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

冧九 2024-09-14 06:55:55

我发现如果你这样做的话它会起作用(见下文)。关键是使用方法:
- [NSDateFormatter getObjectValue:forString:range:error:]

而不是

-[NSDateFormatter dateFromString]

完整代码:

+ (NSDate *)parseRFC3339Date:(NSString *)dateString 
{
    NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init];
    [rfc3339TimestampFormatterWithTimeZone setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    [rfc3339TimestampFormatterWithTimeZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

    NSDate *theDate = nil;
    NSError *error = nil; 
    if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) {
        NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
    }

    [rfc3339TimestampFormatterWithTimeZone release];
    return theDate;
}

I found out it works if you do it this way (see below). The key is using the method:
- [NSDateFormatter getObjectValue:forString:range:error:]

instead of

-[NSDateFormatter dateFromString]

The complete code:

+ (NSDate *)parseRFC3339Date:(NSString *)dateString 
{
    NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init];
    [rfc3339TimestampFormatterWithTimeZone setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    [rfc3339TimestampFormatterWithTimeZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

    NSDate *theDate = nil;
    NSError *error = nil; 
    if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) {
        NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
    }

    [rfc3339TimestampFormatterWithTimeZone release];
    return theDate;
}
℡Ms空城旧梦 2024-09-14 06:55:55

您的设备设置为 24 小时制还是 12 小时制?

这听起来像是一个疯狂的问题,但我刚刚遇到了这个错误 - 日期格式化程序将根据当前区域设置调整您的格式字符串,其中包括时间格式设置。

您可以通过添加此行来强制它忽略它们:

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

希望有帮助。

Is your device set to 24 hour or 12 hour clock?

That sounds like an insane question but I've just run into that bug - the dateformatter will adjust your format string according to the current locale which will include the time format settings.

You can force it to ignore them by adding this line :

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

Hope that helps.

赤濁 2024-09-14 06:55:55

此代码将删除多余的冒号,如 AtomRiot 所描述的:

转换自:

  • NSString *stringDate = @"2010-06-21T20:06:36+00:00";

致:

  • NSString *stringDate = @"2010-06-21T20:06:36+0000";
// Remove colon in timezone as iOS 4+ NSDateFormatter breaks
if (stringDate.length > 20) {
    stringDate = [stringDate stringByReplacingOccurrencesOfString:@":"
                                                       withString:@""
                                                          options:0
                                                            range:NSMakeRange(20, stringDate.length-20)];
}

有关更多详细信息,请参阅:https://devforums.apple.com/thread/45837

This code will remove the extra colon as AtomRiot describes:

Converting it from:

  • NSString *stringDate = @"2010-06-21T20:06:36+00:00";

to:

  • NSString *stringDate = @"2010-06-21T20:06:36+0000";
// Remove colon in timezone as iOS 4+ NSDateFormatter breaks
if (stringDate.length > 20) {
    stringDate = [stringDate stringByReplacingOccurrencesOfString:@":"
                                                       withString:@""
                                                          options:0
                                                            range:NSMakeRange(20, stringDate.length-20)];
}

for more details see: https://devforums.apple.com/thread/45837

走走停停 2024-09-14 06:55:55

我最近遇到了这个问题。我最终使用了 Peter Hosey 的 ISO8601 解析器。它可以在这里找到:http://boredzo.org/iso8601unparser/

I ran into this issue recently. I ended up using Peter Hosey's ISO8601 parser. It is available here: http://boredzo.org/iso8601unparser/

守不住的情 2024-09-14 06:55:55

看起来苹果文档很“棘手”:

格式字符串使用的格式
Unicode 标准中的模式
(此参考版本为tr35-6
适用于 Mac OS X v10.5; 适用于 Mac OS X v10.4
使用版本 tr35-4
)。

iOS:v10.0兼容模式为
不适用于 iOS - 仅10.4
模式可用。

根据版本 tr35-4

使用 1 表示 GMT 格式,使用 2 表示 RFC 822

Z{1} GMT-08:00
Z{2} -0800

但根据 Unicode 标准:

RFC 822 使用一到三个字母,GMT 格式使用四个字母。

Z{1,3} -0800
Z{4} GMT-08:00

所以看起来 iOS 4 正在使用 tr35-6 - Unicode 标准,这就是为什么 +00:00 现在无法对抗 ' Z'。

我在 NSDateFormatter 中尝试 -08:00 针对“ZZZZ”,但在 iOS 4 中失败。但是 GMT-08:00 确实如此工作。时区 (GMT) 现在似乎是必需的,而不是像 iOS 3 中那样是可选的。

It seems like the Apple documentation is, well, 'tricky':

The format string uses the format
patterns from the Unicode standard
(this reference is to version tr35-6
for Mac OS X v10.5; for Mac OS X v10.4
use version tr35-4
).

and

iOS: The v10.0 compatibility mode is
not available on iOS—only the 10.4
mode is available.

According to version tr35-4:

Use 1 for GMT format, 2 for RFC 822

Z{1} GMT-08:00
Z{2} -0800

But according to the Unicode standard:

Use one to three letters for RFC 822, four letters for GMT format.

Z{1,3} -0800
Z{4} GMT-08:00

So it looks like iOS 4 is using tr35-6 - the Unicode standard, which is why +00:00 now fails against 'Z'.

I tried -08:00 against 'ZZZZ' in my NSDateFormatter and it failed in iOS 4. GMT-08:00, however, did work. It seems the timezone (GMT) is now required, rather than optional as it may have been in iOS 3.

养猫人 2024-09-14 06:55:55

我得到的格式介于 RFC 822 和 GMT 之间。如果我将“+00:00”更改为“+0000”,那么我可以在我的格式中使用“Z”。如果我将其更改为“GMT+00:00”,那么我可以使用 ZZZZ 正确获取数据。似乎已经删除了一些东西来处理这种混合,因为它之前在 OS 3.x 上为我工作过。

The format that I am being given is halfway between RFC 822 and GMT. if i change the "+00:00" to "+0000" then I can use the "Z" on my format. and if i change it to "GMT+00:00" then I can use ZZZZ to get the data properly. It seems that something has been stripped out to handle this hybrid as it was working for me before with OS 3.x.

木格 2024-09-14 06:55:55

我从 dataFormat @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'hh:mm"; 获取值

I get the value from dataFormat @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'hh:mm";

老旧海报 2024-09-14 06:55:55

对我来说看起来不错。

它在真实设备上仍然可以正常运行吗?

如果是这样,请提交模拟器的错误报告,否则请提交 iOS 4 的错误报告。

Looks fine to me.

Does it still function correctly on a real device?

If so file a bug report for the simulator, otherwise file a bug report for iOS 4.

早乙女 2024-09-14 06:55:55

我只是在调试这个完全相同的问题。我有与你相同的日期字符串,适用于 3.x 而不是 4.0。症状相同。

当浏览 NSDateFormatter 文档时,我看到:

初始化日期格式化程序
– init 在 iPhone OS 2.0 到 iPhone OS 3.2 中可用

这表示 iOS 4.0 已弃用 init 方法。我不确定这是什么意思。

I was just debugging this exact same problem. I have the same date string as you that works in 3.x and not 4.0. Same symptoms.

When looking through the NSDateFormatter documentation I see:

Initializing a Date Formatter
– init Available in iPhone OS 2.0 through iPhone OS 3.2

This says that the init method has been deprecated for iOS 4.0. I'm not sure what that means.

愁杀 2024-09-14 06:55:55

艾尔弗雷德发布的链接成功了。我在将应用程序从 3.1.3 转换为 iOS4 时偶然发现了同样的问题。该链接包含 ISO8601DateFormatter 类,它是比我自己的日期实用程序类更优秀的扩展。

Elfred 写道: 我最近遇到了这个问题。我最终使用了 Peter Hosey 的 ISO8601 解析器。它可以在这里找到:http://boredzo.org/iso8601unparser/

The link Elfred posted did the trick. I stumbled upon the same issue whilst converting my app from 3.1.3 to iOS4. The link holds the ISO8601DateFormatter class which is a far more excellent extension then my own date utility class.

Elfred wrote: I ran into this issue recently. I ended up using Peter Hosey's ISO8601 parser. It is available here: http://boredzo.org/iso8601unparser/

一杯敬自由 2024-09-14 06:55:55

看来 NSDateFormatter 变得非常挑剔。

-(void)dateFormatterTests {
    NSDateFormatter *formatter;

    formatter = [[NSDateFormatter alloc] init];

#ifdef WORKS
    [formatter setDateFormat:@"yyyy-MM-dd"];
#elif defined(ALSO_WORKS)
    [formatter setDateFormat:@"yyyy MM dd"];
    [formatter setLenient:YES];
#else // DOESN'T WORK
    [formatter setDateFormat:@"yyyy MM dd"];
#endif

    // Works per comments above
    NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13"]);  
    // Never works with any of the above formats
    NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13 22:00"]); 

    [formatter release]; formatter = nil;
}

It seems the NSDateFormatter has gotten very picky.

-(void)dateFormatterTests {
    NSDateFormatter *formatter;

    formatter = [[NSDateFormatter alloc] init];

#ifdef WORKS
    [formatter setDateFormat:@"yyyy-MM-dd"];
#elif defined(ALSO_WORKS)
    [formatter setDateFormat:@"yyyy MM dd"];
    [formatter setLenient:YES];
#else // DOESN'T WORK
    [formatter setDateFormat:@"yyyy MM dd"];
#endif

    // Works per comments above
    NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13"]);  
    // Never works with any of the above formats
    NSLog(@"dFS: %@", [formatter dateFromString:@"2010-01-13 22:00"]); 

    [formatter release]; formatter = nil;
}
她说她爱他 2024-09-14 06:55:55

我在我的应用程序中也遇到了同样的问题来自 NSDateFormatter 的空值

我发现我的问题如下:

我向我的应用程序发送了这样的日期和时间:07/16/2010 04:21:00 +00:00,格式化程序如下:[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss ZZZ"]

看来格式化的 ZZZ 部分不再接受冒号 : 。

有效:07/16/2010 04:21:00 +0000

无效:07/16/2010 04:21:00 +00:00

支持对于当前已推出的应用程序,我所做的只是搜索字符串中的 +00:00 部分并将其替换为 +0000

希望这可以帮助其他人。

I had the same issue in my apps as well Null Value from NSDateFormatter.

I found my problem to be the following:

I was sending my app a date and time like this: 07/16/2010 04:21:00 +00:00 with the formatter like this: [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss ZZZ"]

It seems like the ZZZ part of the formating NO LONGER accepts the colon : in the time.

Works: 07/16/2010 04:21:00 +0000

Doesn't work: 07/16/2010 04:21:00 +00:00

To support the current apps that are out, all I did was search for the +00:00 part in the string and replace it with +0000.

Hope this might help others.

Saygoodbye 2024-09-14 06:55:55

我使用它的方式很简单:

date_formatter = [[NSDateFormatter alloc] init];
        [date_formatter setDateStyle:NSDateFormatterShortStyle];
        [date_formatter setTimeStyle:NSDateFormatterNoStyle];

而且效果很好。我不明白当 NSDateFormatter 类是 NSObject 的子类时,他们如何弃用 init 方法

I'm using it as simple as:

date_formatter = [[NSDateFormatter alloc] init];
        [date_formatter setDateStyle:NSDateFormatterShortStyle];
        [date_formatter setTimeStyle:NSDateFormatterNoStyle];

and this is working great. I don't understand how can they deprecate the init method, when the NSDateFormatter class is subclassed from NSObject

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文