2013-01-17 64 views
0

這裏是我的代碼:的NSDate倒計時與NSInterval

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]) { 

} 

問題是, '如果([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基本上是當前時間的最近一個分鐘的四捨五入。此外,整個代碼段每隔1秒鐘處於一個重複的NSTimer中。

我該如何解決這個問題?

+0

你能解釋一下你的代碼是應該做的? –

+0

基本上,當我按下一個按鈕時,它會設置一個NSTimer來每隔1秒觸發我的代碼。它正在等待下一個「回合時間」。比如,如果是10:31:23那麼它會倒數到10:32:00。如果時間是01:10:01那麼它會倒計時到01:11:00 – objectiveccoder001

+0

然後,當倒計時結束時,如果([currentTime isEqualToString:nextTrigger]),其他代碼將觸發。 – objectiveccoder001

回答

-1

我有一個類似的問題。我所做的就是創建一個副本,並通過檢查。(有一件事是同時擁有oldTriggernewTrigger已申報)檢查這個代碼

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]; 
oldTrigger = nextTrigger; 
nextTrigger = [dateFormatter stringFromDate:nextIntervalDate]; 

timeRemaining.text = [NSString stringWithFormat:@"%02d:%02d", secondsLeft/60, secondsLeft%60]; 

NSLog([NSString stringWithFormat:@"%@:%@", currentTime, nextTrigger]); 

if ([currentTime isEqualToString:oldTrigger]) { 

} 
+0

這會導致應用程序因某種原因崩潰。 Thread1:EXEC_BAD_ACCESS在這一行:if([currentTime isEqualToString:oldTrigger]){ – objectiveccoder001

+0

Put nextTrigger = [dateFormatter stringFromDate:nextIntervalDate]; 之前oldTrigger = nextTrigger,並沒有導致崩潰,但唉,oldtrigger和當前時間仍然不相等 – objectiveccoder001

+0

這項工作?他們之所以不相同的原因是因爲newTrigger更新到快速。如果這不是錯誤,那麼確保oldTrigger = nextTrigger在nextTrigger = [dateFormatter stringFromDate:nextIntervalDate]之前; – BigT

0

如果你有代碼想在下一整分鐘過後觸發,那麼重複檢查以確定是否已經過去了,效率很低。將該代碼放在自己的方法中,並創建一個NSTimer以在適當的時候觸發它。

NSDate* date = [NSDate date] ; 
NSCalendar* calendar = [NSCalendar currentCalendar] ; 

NSDateComponents* secondsComponents = [calendar components:NSSecondCalendarUnit fromDate:date] ; 
NSTimeInterval secondsUntilNextWholeMinute = 60 - secondsComponents.second ; 
[NSTimer scheduledTimerWithTimeInterval:secondsUntilNextWholeMinute target:self selector:@selector(yourMethod) userInfo:nil repeats:NO ] ; 
+0

哦,忘了提及,我在這裏使NSTimer失效,如果([currentTime isEqualToString:oldTrigger]){所以它只發生一次。雖然謝謝! – objectiveccoder001

+0

我的意思是,而不是讓你的代碼每隔0.1秒運行一次以檢查下一整分鐘是否已經過去,你可以簡單地告訴iOS在特定時間運行某種方法。 –

+0

啊!我喜歡。讓我試着去實現它。 – objectiveccoder001