提示创建失败,错误消息为NIL

时间:2013-06-04 02:53:42

标签: ios error-handling eventkit ios6.1 reminders

我一直在努力为我的应用添加待办事项功能。虽然看起来一切看起来都正确但我在Reminder上创建失败,但错误值为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显示以下内容: “保存提醒错误:( null)”

1 个答案:

答案 0 :(得分:1)

将以下代码添加到ViewDidLoad函数

   // Add Todo Feature
    store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeReminder
                          completion:^(BOOL granted, NSError *error) {
                              // Handle not being granted permission
                          }];