我编写了这段代码,通过 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/