2010-05-31 28 views

回答

2

該表達式不會返回一個NSString,但沒有格式。要正確格式化的NSString,使用+stringWithFormat: method

NSString* formattedDate = [NSString stringWithFormat:@"Date for today %@:", 
          [dateFormatter stringFromDate:today]]; 

表達a, b是 「comma expression」。它將依次評估ab,並返回值b。在你的情況下,它將只返回日期字符串。

大多數情況下,您不想使用逗號表達式。


而且,dateFormatter需要使用前配置。例如,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeStyle:NSDateFormatterNoStyle]; 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
... 
... [dateFormatter stringFromDate:today] ... 
... 
[dateFormatter release]; 
+0

我剛剛嘗試過,它返回「今日日期:」沒有實際日期,但? – TheLearner 2010-05-31 15:08:40

+0

@The:你需要調用'-setTimeStyle:使用前'爲dateFormatter:'和'-setDateStyle。 – kennytm 2010-05-31 15:10:14

相關問題