2013-05-03 70 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