2010-07-16 39 views
0

在使用iOS4.0編譯之前,這工作正常,我無法弄清楚什麼是錯的。來自XML的NSDateFormatter的NULL值

這是問題所在。我從我的數據庫中發送我的應用程序分數與日期/時間。我的目標是將其存儲在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) 

任何幫助表示讚賞!謝謝。

回答

2

我發現這個問題。似乎ZZZ部分的格式化不成立時接受冒號:

作品:07/16/2010 04:21:00 +0000

不起作用:07/16/2010 04:21:00 +00:00

支持當前的應用程序,都出來了,我所做的只是搜索字符串中的+00:00一部分,並與+0000更換。 TADA!有用!

0

NSDateFormatter在iOS4中變得非常挑剔。 This other SO question有同樣的問題,並有幾個不同的解決方案。

0

我有同樣的問題。對我來說,問題是,我在爲我的最新XML飼料換行,因此,這段代碼的固定對我來說:

[currentDate replaceOccurrencesOfString:@"\n" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[currentDate length])];