2012-01-10 68 views
0

我有NSDateFormatter和iOS的問題。我居然發現了一個認可和這裏的問題的解釋:iOS 5打破NSDateFormatter?

http://www.cocoanetics.com/2011/12/ios-5-breaking-nsdateformatter/

然而,我的大腦是無法處理這些信息翻譯所有爲我的特定情況下的解決方案。我希望你們的朋友們能夠幫助我:

任何人都可以告訴我如何在iOS5中做以下工作嗎?它曾經被做工精細:

NSLocale *myLocale = [[NSLocale alloc] initWithLocaleIdentifier:@」en_GB」]; 
[inputFormatter setLocale: myLocale]; 
[myLocale release]; 
[inputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"]; 
NSDate *formattedDate = [inputFormatter dateFromString: feedDateString]; 
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; 
[outputFormatter setDateFormat:@"EEEE d MMMM"]; 

輸入的字符串,這是我無法控制,會像「星期二,2012年1月3日00:00:00 +0100「 當我登錄[outputFormatter stringFromDate:格式化日期]我得到空

原始的語言環境是「en_US」,但我改變了這一點,希望這會有所幫助。

這也將有助於在這個職位的人來說,這似乎是一條死衚衕後:

NSDateFormatter on ios 5 - any other way to use?

回答

0

給大家花了腦時間在這:這完全是我自己的愚蠢錯誤: 輸入字符串需要進行格式化並沒有「星期一,2012年2月13日00:00:00 +0100 「但它實際上是」2012年2月13日星期一00:00:00 +0100 「 你看?那麼,我沒有看到NSLogging期間的尾隨空格。 [反諷:許多空間和返回後,字符串都不顯示在這裏!]

而不知何故,iOS 4並不關心它。 iOS5只是更挑剔,可能是正確的。 所以,我解決了這個事情如下:

NSString *trimmedString = [inputstring stringByTrimmingCharactersInSet: 
           [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

我希望這能幫助到你。

0

試試這個:

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; 
[outputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"]; 
        
NSString *str = [outputFormatter stringFromDate:[NSDate date]]; 
/* 
    Do something with this string 
*/ 
[outputFormatter release]; 

你也可以通過添加加地區setLocale parameter to NSDateFormatter。

NSLocale *myLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]; 
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; 
[outputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"]; 
[outputFormatter setLocale: myLocale]; 
        
NSString *str = [outputFormatter stringFromDate:[NSDate date]]; 

/* 
    Do something with this string 
*/ 

[outputFormatter release]; 
[myLocale release]; 
+0

感謝,不幸的是,它不能解決我的問題。在你的例子中,你使用[NSDate date]。有了這個值,它的工作正常。所以,顯然,我必須檢查我的inputFormatter。 – Sjakelien 2012-01-26 13:16:23