我正在開始爲iPhone開發,因此我在網上查看不同的教程以及嘗試一些不同的東西。目前,我正在嘗試創建一個倒計時,直到午夜。要獲得的小時,分鍾和秒,我做了以下的數量(這是我發現的地方):每一個地方我用-dateWithCalendarFormat:timeZone:
我如何獲得可可的當前日期
NSDate* now = [NSDate date];
int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
不過,我得到以下錯誤:
warning: 'NSDate' may not respond to '-dateWithCalendarFormat:timeZone:'
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
warning: no '-hourOfDay' method found
error: invalid operands to binary - (have 'int' and 'id')
這看起來很簡單。我錯過了什麼?
另外,我已經注意到在不同的地方和不同的時間星號(*)位於時間NSDate* now
之後,或者位於變量NSDate *now
之前。兩者有什麼區別,爲什麼你會使用一個與另一個?
你爲什麼要使用NSCalendar這是最近的更改?有很多代碼使用NSDate來處理這些事情。 – Jason 2009-07-01 17:40:04
是的,許多方法現在已被棄用。 – 2009-07-01 17:51:00
傑森:NSDate依然存在。您使用NSCalendarDate(這就是-dateWithCalendarFormat:時區:收益),並計劃棄用,並在iPhone上不存在。 – 2009-07-02 02:32:33