字符串崩溃的日期导致设备崩溃 [英] date from string crashes device

查看:65
本文介绍了字符串崩溃的日期导致设备崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读RSS,并从中读取一个元素: < pubDate> 星期四,2011年4月7日13:37:41 +0000 </pubDate> 我使用以下代码将接收到的字符串转换为NSDate

I am reading a RSS and from there an element: <pubDate>Thu, 07 Apr 2011 13:37:41 +0000</pubDate> I use the following code to turn the received string into NSDate

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  

        [dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700 


        NSDate *date = [dateFormatter dateFromString:[self.currentDate stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];        

        [item setObject:date forKey:@"date"]; 

代码在模拟器中运行正常,但在设备上崩溃.原因是NSDate对象保持为零,当我将其添加到字典中时,它使应用程序崩溃.

Code's working perfectly in simulator but on device it crashes. The reason is that NSDate object stays nil and when I'm adding it to the dictionary it crashes the app.

关于这个问题,我读了很多书,但是没有一个解决方案对我有用.我的意思是很多人都做过,但是在模拟器中……

I read a lot around about this problem but none of the solutions worked for me. I mean lots of them did, but in simulator...

任何帮助表示赞赏,卢卡...

Any help appreciated, Luka ...

推荐答案

检查两件事,以消除模拟器和ios设备之间的差异.

Check two things to eliminate the differences between simulator and ios device.

  1. iOS设备区分大小写.因此,您必须检查字符串是否有问题.

  1. iOS Device is case sensitive. So you have to check your string if there is something wrong.

语言环境设置可能与您的模拟器和iOS不同.以防止定义日期格式化程序的正确语言环境.

Locale setting can be different with your simulator and iOS. to prevent this define proper locale of date formatter.

NSLocale * usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

[dateFormatter setLocale:usLocale];

[dateFormatter setLocale:usLocale];

这篇关于字符串崩溃的日期导致设备崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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