这是用于安排警报的以下代码,我收到以下错误。在我尝试将当前日期与选择器进行比较之前,它一直运行良好:
[self.eventText resignFirstResponder];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//Get the current date
NSDate *pickerDate = [self.datePicker date];
//Unable to set notification for same day
[datePicker setMinimumDate:[NSDate date]];
//Break the date up into components
NSDateComponents *dateComponents = [calendar components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components: (NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:pickerDate];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = datePicker;
localNotification.alertBody = self.eventText.text;
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];
eventText.text = nil;
[self dismissViewControllerAnimated:YES completion:nil];
收到以下错误:由于未捕获的异常NSInvalidArgumentException
而终止应用,原因为:-[UIDatePicker copyWithZone:]: unrecognized selector sent to instance 0x10ba853f0
。我在调试方面很糟糕,还没有找到解决方案。
答案 0 :(得分:1)
我相信你遇到的问题就在于这一行。
localNotification.fireDate = datePicker;
您没有提供定义" datePicker"的上述代码,但我的猜测是您打算键入" pickerDate"而不是" datePicker",它不符合NSCopying协议(并且无论如何都不是NSDate),这就是抛出错误的原因。