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按钮时,我收到这个输出,这意味着它工作正常,但是,当时间到来时,我没有得到任何输出说通知已被取消或重新安排,这意味着它不工作!!!

输出:

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

我错过了什么?

另外,这会在后台工作吗?

4

1 回答 1

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 类别,但这比我认为您需要的要复杂。

于 2013-05-03T01:29:52.917 回答