我在此代码上工作,通过睡眠时间为我的通知执行暂停,但是有些东西缺少,我无法弄清楚。会是什么呢?时间比较不起作用。什么可能是错的?
我.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
我缺少什么?
另外,这会在后台工作吗?
你为什么期望这段代码有效?您正试图将开始时间与“now”进行比较,并将结束时间与“now”进行比较。开始时间和结束时间几乎不可能与相应的“现在”相等。一个'NSDate'对象下降到微秒。 – rmaddy 2013-05-03 01:01:35
如果你有一个anaswer,请告诉我!我如何将它与现在相同? – user1949873 2013-05-03 10:00:59
我没有一个完整的答案,因为它不清楚你想做什么。如何设置_startTime.date和_endTime.date?在你的两个if语句中,你真的想要比较什么?只是'NSDate'的日期部分或日期和时间?以什么决议?并且不要两次调用[[NSDate date]]。调用一次并将其保存在一个变量中。然后将开始日期和结束日期与一个值进行比较。 – rmaddy 2013-05-03 14:36:12