我一直在努力为我的应用程序添加待办事项功能。虽然一切看起来都正确,但我在提醒上创建失败,但错误值为 NIL。这是相关代码 - (我从 UIViewController 中提取文本和日期 - 一切都正确连接)。
-(IBAction)createButtonPressed
{
// Setup Variables for coverting date:
int year;
int month;
int day;
int hour;
int minute;
int second;
NSDate *actualDate;
NSDateComponents *dateComps;
NSCalendar *calendar;
NSDateFormatter *dateFormatter;
actualDate = dateValue.date;
calendar = [[NSCalendar currentCalendar] copy];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[calendar timeZone]];
[dateFormatter setDateFormat:@"yyyy"];
year = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"MM"];
month = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"dd"];
day = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"HH"];
hour = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"mm"];
minute = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"ss"];
second = [[dateFormatter stringFromDate:actualDate] intValue];
dateComps = [[NSDateComponents alloc] init];
[dateComps setTimeZone:[calendar timeZone]];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
// Create the Todo
EKReminder *reminder = [EKReminder reminderWithEventStore:store];
[reminder setTitle:self.actionText.text];
[reminder setDueDateComponents:dateComps];
EKCalendar *defaultReminderList = [store defaultCalendarForNewReminders];
[reminder setCalendar:defaultReminderList];
NSError *error = nil;
BOOL success = [store saveReminder:reminder
commit:YES
error:&error];
if (!success) {
NSLog(@"Error saving reminder: %@", [error localizedDescription]);
// Popup a messaging saying the reason why you can't create the todo.
} else {
// Popup a message saying the to do was created
}
}
`这段代码的大部分是试图以正确的格式获取日期。我的 NSLog 显示以下内容:“错误保存提醒:(空)”