来自 XML 的 NSDateFormatter 的 NULL 值
在用 iOS4.0 编译之前,这工作得很好,我不知道出了什么问题。
问题就在这里。我从数据库中发送带有日期/时间的应用程序分数。我的目标是将其存储在 CoreData 中。当我使用下面的代码时,我得到的日期为空值。
代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"US"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss ZZZ"];
currentDownloadedScore.dateEntered = [dateFormatter dateFromString:self.currentValue];
NSLog(@"Date CV: %@",self.currentValue);
NSLog(@"Date: %@",[currentDownloadedScore.dateEntered description]);
[locale release];
[dateFormatter release];
这是调试器中的结果:
2010-07-16 08:15:35.741 MyApp[75003:207] Date CV: 07/16/2010 04:21:00 +00:00
2010-07-16 08:15:35.742 MyApp[75003:207] Date: (null)
2010-07-16 08:15:35.745 MyApp[75003:207] Date CV: 07/16/2010 01:09:26 +00:00
2010-07-16 08:15:35.749 MyApp[75003:207] Date: (null)
感谢任何帮助!谢谢。
This was working fine before compiling with iOS4.0 and I can't figure out what's wrong.
Here's the problem. I send my app scores from my database with a date/time on it. My goal is to store that in CoreData. When I use the code below I get a null value for the date.
Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"US"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss ZZZ"];
currentDownloadedScore.dateEntered = [dateFormatter dateFromString:self.currentValue];
NSLog(@"Date CV: %@",self.currentValue);
NSLog(@"Date: %@",[currentDownloadedScore.dateEntered description]);
[locale release];
[dateFormatter release];
Here's the result in the debugger:
2010-07-16 08:15:35.741 MyApp[75003:207] Date CV: 07/16/2010 04:21:00 +00:00
2010-07-16 08:15:35.742 MyApp[75003:207] Date: (null)
2010-07-16 08:15:35.745 MyApp[75003:207] Date CV: 07/16/2010 01:09:26 +00:00
2010-07-16 08:15:35.749 MyApp[75003:207] Date: (null)
Any help is appreciated! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现了这个问题。看来格式化的
ZZZ
部分不再接受冒号:
。有效:
07/16/2010 04:21:00 +0000
无效:
07/16/2010 04:21:00 +00:00
支持对于当前已推出的应用程序,我所做的只是搜索字符串中的
+00:00
部分并将其替换为+0000
。田田!有用!I found the issue. 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
. TADA! It works!NSDateFormatter 在 iOS4 中变得非常挑剔。 这个其他问题也有同样的问题,并且有几个不同的解决方案。
NSDateFormatter has gotten very picky in iOS4. This other SO question had the same problem and has a couple different solutions.
我也有同样的问题。对我来说,问题是我的 XML feed 中的日期有一个换行符,所以这段代码为我修复了它:
[currentDate ReplaceOccurrencesOfString:@"\n" withString:@"" options:NSCaseInsensitiveSearch range: NSMakeRange(0,[当前日期长度])];
I had the same problem. For me, the problem was that I had a line break in my XML feed for the date, so this code fixed it for me:
[currentDate replaceOccurrencesOfString:@"\n" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[currentDate length])];