这是我的代码:
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSString *currentTime = [dateFormatter stringFromDate:currDate];
NSDate *now = [NSDate date];
int interval = 1*60; // one minute (minutes*60)
long int nowSeconds = (long int) [now timeIntervalSince1970];
int secondsLeft = interval - (nowSeconds % interval);
NSDate *nextIntervalDate = [NSDate dateWithTimeIntervalSince1970:nowSeconds+secondsLeft];
NSString *nextTrigger = [dateFormatter stringFromDate:nextIntervalDate];
timeRemaining.text = [NSString stringWithFormat:@"%02d:%02d", secondsLeft/60, secondsLeft%60];
NSLog([NSString stringWithFormat:@"%@:%@", currentTime, nextTrigger]);
if ([currentTime isEqualToString:nextTrigger]) {
}
问题是,'if ([currentTime isEqualToString:nextTrigger])' 永远不相等。因为 nextTrigger 在它们相等之前更改为下一个时间段。这是NSLog:
2013-01-17 15:54:59.987 app[35987:c07] 15:54:59:15:55:00
2013-01-17 15:54:59.997 app[35987:c07] 15:54:59:15:55:00
2013-01-17 15:55:00.007 app[35987:c07] 15:55:00:15:56:00 <----RIGHT HERE
2013-01-17 15:55:00.016 app[35987:c07] 15:55:00:15:56:00
我的代码的 nextTrigger 基本上是将当前时间按最近的分钟向上取整。此外,整个代码段每 0.1 秒重复一次 NSTimer。
我怎样才能解决这个问题?