ios - 时间比较不起作用。有什么问题吗?

标签 ios objective-c cocoa-touch background notifications

我编写了这段代码,通过 sleep 时间为我的通知执行暂停,但是缺少一些我不太明白的东西。会是什么?

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

我错过了什么?

此外,这会在后台运行吗?

最佳答案

你需要一个不同的比较。也许是这样的: (以下所有内容都应该在您的 .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 类别,但这比我认为你需要的更复杂。

关于ios - 时间比较不起作用。有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16349813/

相关文章:

ios - 进一步扩展可扩展表格 View 单元格

ios - Swift iOS 和 Java Sockets 之间的 TCP 通信

iphone - 这个对象是如何被过早释放的?

iphone - 选择时 Xcode UITableView 行未突出显示

ios - 当用户点击 View 外的任何地方时关闭 View

iphone - 无需按任何按钮即可从 View 1 切换到 View 2

ios - 尺寸错误的 UIImagePickerController View

objective-c - OCLint 在 html 报告文件中出现编译错误,但我的项目构建成功。为什么

iphone - 当当前选定的表格单元格移出屏幕后,如何向其发送消息?

ios - 测试两个整数以确定现在是白天还是晚上