NSDateFormatter 在 OS 4.0 中返回 nil
我在 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 使用这些特定方法进行不同实现的内容。我遗漏了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我发现如果你这样做的话它会起作用(见下文)。关键是使用方法:
- [NSDateFormatter getObjectValue:forString:range:error:]
而不是
-[NSDateFormatter dateFromString]
完整代码:
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:
您的设备设置为 24 小时制还是 12 小时制?
这听起来像是一个疯狂的问题,但我刚刚遇到了这个错误 - 日期格式化程序将根据当前区域设置调整您的格式字符串,其中包括时间格式设置。
您可以通过添加此行来强制它忽略它们:
希望有帮助。
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 :
Hope that helps.
此代码将删除多余的冒号,如 AtomRiot 所描述的:
有关更多详细信息,请参阅:https://devforums.apple.com/thread/45837
This code will remove the extra colon as AtomRiot describes:
for more details see: https://devforums.apple.com/thread/45837
我最近遇到了这个问题。我最终使用了 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/
看起来苹果文档很“棘手”:
和
根据版本 tr35-4:
但根据 Unicode 标准:
所以看起来 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':
and
According to version tr35-4:
But according to the Unicode standard:
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.
我得到的格式介于 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.
我从 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"
;对我来说看起来不错。
它在真实设备上仍然可以正常运行吗?
如果是这样,请提交模拟器的错误报告,否则请提交 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.
我只是在调试这个完全相同的问题。我有与你相同的日期字符串,适用于 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.
艾尔弗雷德发布的链接成功了。我在将应用程序从 3.1.3 转换为 iOS4 时偶然发现了同样的问题。该链接包含 ISO8601DateFormatter 类,它是比我自己的日期实用程序类更优秀的扩展。
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.
看来 NSDateFormatter 变得非常挑剔。
It seems the NSDateFormatter has gotten very picky.
我在我的应用程序中也遇到了同样的问题来自 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.
我使用它的方式很简单:
而且效果很好。我不明白当 NSDateFormatter 类是 NSObject 的子类时,他们如何弃用 init 方法
I'm using it as simple as:
and this is working great. I don't understand how can they deprecate the init method, when the NSDateFormatter class is subclassed from NSObject