0

我正在尝试建立一个倒计时,它显示要走的秒数。

下面的示例设置为 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
4

1 回答 1

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

你在使用不同的版本吗?

于 2012-06-05T04:14:12.507 回答