时间比较不起作用。可能有什么不对?

时间:2013-05-03 00:27:04

标签: ios objective-c cocoa-touch background notifications

我在这个代码上工作,通过睡眠时间暂停我的通知,但是有些东西丢失了,我无法弄明白。它会是什么?

我的.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

我错过了什么?

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

1 个答案:

答案 0 :(得分: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类别,但这比我认为你需要的更复杂。