我在此代碼上工作,通過睡眠時間爲我的通知執行暫停,但是有些東西缺少,我無法弄清楚。會是什麼呢?時間比較不起作用。什麼可能是錯的?
我.m
代碼:
- (IBAction)save:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:_startTime.date];
NSLog(@"Start time is %@",dateTimeString);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:_endTime.date];
NSLog(@"End time is %@",dateTimeString2);
if ([[NSDate date] isEqualToDate:_startTime.date]) {
NSLog(@"currentDate is equal to startTime");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(@"Sleep time silent started and notification cancelled");
}
if ([[NSDate date] isEqualToDate:_endTime.date]) {
NSLog(@"currentDate is equal to endTime");
[self pickerSelectedRow];
NSLog(@"Time to wake up and notification rescheduled");
}
}
當我save
按鈕點擊我收到此out輸出,這意味着它的正常工作,但是,在時機成熟時我沒有得到任何輸出並稱通知已取消或重新安排,這意味着它不工作!
輸出:
2013-05-03 03:04:57.481 xxx[10846:c07] Start time is 5/3/13, 3:06 AM
2013-05-03 03:04:57.482 xxx[10846:c07] End time is 5/3/13, 3:07 AM
我缺少什麼?
另外,這會在後臺工作嗎?
你爲什麼期望這段代碼有效?您正試圖將開始時間與「now」進行比較,並將結束時間與「now」進行比較。開始時間和結束時間幾乎不可能與相應的「現在」相等。一個'NSDate'對象下降到微秒。 – rmaddy 2013-05-03 01:01:35
如果你有一個anaswer,請告訴我!我如何將它與現在相同? – user1949873 2013-05-03 10:00:59
我沒有一個完整的答案,因爲它不清楚你想做什麼。如何設置_startTime.date和_endTime.date?在你的兩個if語句中,你真的想要比較什麼?只是'NSDate'的日期部分或日期和時間?以什麼決議?並且不要兩次調用[[NSDate date]]。調用一次並將其保存在一個變量中。然後將開始日期和結束日期與一個值進行比較。 – rmaddy 2013-05-03 14:36:12