2013-05-03 71 views
1

我在此代碼上工作,通過睡眠時間爲我的通知執行暫停,但是有些東西缺少,我無法弄清楚。會是什麼呢?時間比較不起作用。什麼可能是錯的?

.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 

我缺少什麼?

另外,這會在後臺工作嗎?

+0

你爲什麼期望這段代碼有效?您正試圖將開始時間與「now」進行比較,並將結束時間與「now」進行比較。開始時間和結束時間幾乎不可能與相應的「現在」相等。一個'NSDate'對象下降到微秒。 – rmaddy 2013-05-03 01:01:35

+0

如果你有一個anaswer,請告訴我!我如何將它與現在相同? – user1949873 2013-05-03 10:00:59

+0

我沒有一個完整的答案,因爲它不清楚你想做什麼。如何設置_startTime.date和_endTime.date?在你的兩個if語句中,你真的想要比較什麼?只是'NSDate'的日期部分或日期和時間?以什麼決議?並且不要兩次調用[[NSDate date]]。調用一次並將其保存在一個變量中。然後將開始日期和結束日期與一個值進行比較。 – rmaddy 2013-05-03 14:36:12

回答

1

您需要進行不同的比較。也許是這樣的: (下面的一切應該在你的.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]; 
    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]; 
    NSLog(@"End time is %@",dateTimeString2); 

    if ([self date:[NSDate date] compareMe:_startTime]) { 
     NSLog(@"currentDate is equal to startTime"); 

     [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

     NSLog(@"Sleep time silent started and notification cancelled"); 

    } 

    if ([self date:[NSDate date] compareMe:_endTime]) { 
     NSLog(@"currentDate is equal to endTime"); 

     [self pickerSelectedRow]; 

     NSLog(@"Time to wake up and notification rescheduled"); 
    } 
} 

-(BOOL)date:(NSDate *)date1 compareMe:(NSDate *)date2 { 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *date1Componenets = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date1]; 
    NSDateComponents *date2Componenets = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date2]; 

    return [date1Componenets year] == [date2Componenets year]; 
} 

如果你要比較日期了很多,你可以把比較函數在不同的文件,如NSDate類別,但這比我想你需要的更復雜。

+0

謝謝。但是當我複製這段代碼並且它說'使用未聲明的標識符'date''時它失敗了。我試圖在'.h'中聲明,但仍然是一樣的。你能否給我看看你的'.h'。另外,我應該在哪裏調用'if'?在按鈕方法下?或者我應該把它放在'AppDelegate'的'background'方法下? – user1949873 2013-05-03 11:39:52

+0

我設置它,以便比較方法 - (布爾)日期:(NSDate * date1)isEqualToDate:(NSDate * date2)將在同一個.m文件中。看我的編輯。 – HalR 2013-05-03 12:22:33

+0

@HaIR感謝您所做的出色工作。我已經表決了你的答案。但是,我仍然需要將布爾值聲明爲我的'.h',以便測試它。如果你能向我展示你的'.h'或告訴我如何聲明它。即使你有一個教程的鏈接會很好。非常感謝。 – user1949873 2013-05-03 13:36:57