我正在开始为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