我正在尝试建立倒计时,显示要去的秒数。
以下示例设置为10秒,但输出为8秒。
NSDate *currentDate = [NSDate date];
NSDate *finishDate = [NSDate dateWithTimeInterval:10 sinceDate:currentDate];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSSecondCalendarUnit|NSMinuteCalendarUnit|NSHourCalendarUnit | NSDayCalendarUnit| NSMonthCalendarUnit | NSYearCalendarUnit
fromDate:currentDate
toDate:finishDate
options:0];
NSInteger hour_ = [components hour];
NSInteger minute_ = [components minute];
NSInteger second_ = [components second];
日志
currentDate = 2012-06-05 03:13:10 +0000
finishDate_ = 2012-06-05 03:13:19 +0000
second_ = 8
答案 0 :(得分:0)
好吧,我只是复制并粘贴了你的代码。 我将这些行添加到底部:
NSLog(@"%@", currentDate);
NSLog(@"%@", finishDate);
NSLog(@"%d, %d, %d", hour_, minute_, second_);
在运行iOS 5.1的iPad模拟器上使用Xcode 4.3.2,效果很好:
2012-06-05 00:19:34.341 Testing App[59813:fb03] 2012-06-05 04:19:34 +0000
2012-06-05 00:19:34.342 Testing App[59813:fb03] 2012-06-05 04:19:44 +0000
2012-06-05 00:19:34.343 Testing App[59813:fb03] 0, 0, 10
您使用的是不同的版本吗?