我正在开始为iPhone开发,因此我在网上查看不同的教程以及自己尝试一些不同的东西.目前,我正在努力创建一个倒计时,直到午夜.要获得小时,分钟和秒的数量,我执行以下操作(我在某处找到):
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];
但是,我使用的每个地方-dateWithCalendarFormat:timeZone:
都会出现以下错误:
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
.这两者有什么区别,为什么你会使用一个与另一个?
您有iOS 4.2的问题吗?使用此代码:
NSDate *currDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:currDate]; NSLog(@"%@",dateString);
- > 20.01.2011 10:36:02
您必须使用以下内容:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourDateHere]; NSInteger hour = [dateComponents hour]; NSInteger minute = [dateComponents minute]; NSInteger second = [dateComponents second]; [gregorian release];
现在NSDate*和NSDate*之间没有区别,这只是一个偏好问题.从编译器的角度看,没有任何变化.
您还可以使用:
CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem()); countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02.0f", currentDate.hour, currentDate.minute, currentDate.second]; CFRelease(currentDate); // Don't forget this! VERY important
我认为这有以下优点:
没有直接的内存分配.
秒是双精度而不是整数.
没有消息呼叫.
快点.
//您可以使用以下代码获取当前日期/时间:
NSDate* date = [NSDate date]; NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone]; formatter.timeZone = destinationTimeZone; [formatter setDateStyle:NSDateFormatterLongStyle]; [formatter setDateFormat:@"MM/dd/yyyy hh:mma"]; NSString* dateString = [formatter stringFromDate:date];
替换这个:
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];
有了这个:
NSDate* now = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now]; NSInteger hour = [dateComponents hour]; NSInteger minute = [dateComponents minute]; NSInteger second = [dateComponents second]; [gregorian release]; countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second];