开发者

date from string crashes device

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"]; 

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 h开发者_运维百科elp appreciated, Luka ...


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

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

  2. 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"];

    [dateFormatter setLocale:usLocale];


Test your formatter's behaviour by

[dateFormatter dateFromString:[NSDate date]]

If it's not the formatter's format it means that your nsstring is the one to blame.

Could you log the string that you are trying to convert? It's probably the one at fault

NSLog(@"string is: %@", self.currentDate);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜