2

我收到一个错误:

删除事件错误:Error Domain=EKErrorDomain Code=11“该事件不属于该事件存储。” UserInfo=0x1fdf96b0 {NSLocalizedDescription=该事件不属于该事件存储。

当我尝试删除我刚刚创建的 EKEvent 时。

下面的代码显示我正在存储 eventIdentifier 并使用它来检索事件。此外,当我这样做时,NSLog 事件我可以正确地看到它的所有属性。

从我看到的所有例子中,我做的一切都是正确的。我还对 EKEventStore 的 eventStoreIdentifier 进行了 NSLog 处理,并且每次我在任何方法中访问它时它都是相同的,因此它应该是相同的 EKEventStore。

任何帮助将不胜感激。

- (EKEvent *) getCurrentCalendarEvent {
    NSString *currentCalendarEventID = [[UserModel sharedManager] currentCalendarEventID];
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [eventStore eventWithIdentifier:currentCalendarEventID];
    return event;
}

- (void) removeCurrentCalendarEvent {
    EKEvent *event = [self getCurrentCalendarEvent];
    if (event) {
        NSError *error;
        EKEventStore *eventStore = [[EKEventStore alloc] init];
        [eventStore removeEvent:event span:EKSpanFutureEvents error:&error];
    }
}

- (void) addCurrentCalendarEvent {
    [self removeCurrentCalendarEvent];

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.title = [[UserModel sharedManager] reminderLabel];
    event.notes = @"Notes";

    NSDate *startDate = [NSDate date];
    int futureDateSecs = 60 * 5;
    NSDate *endDate = [startDate dateByAddingTimeInterval:futureDateSecs];

    event.startDate = startDate;
    event.endDate = endDate;

    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

    NSError *error;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&error];

    [[UserModel sharedManager] setCurrentCalendarEventID:event.eventIdentifier];
    [[UserModel sharedManager] saveToDefaults];
}
4

1 回答 1

7

这是因为你是always initializing a new instance of EKEventStore。当您添加EKEventEKEventStore,实例与EKEventStore您尝试删除时不同。您可以做的是EKEventStore在 .h 中声明引用变量并仅对其进行一次初始化。

在.h -

EKEventStore *eventStore;

在.m -

里面viewDidLoad——

eventStore = [[EKEventStore alloc] init];

然后从所有三种方法中删除此行-

EKEventStore *eventStore = [[EKEventStore alloc] init];
于 2012-05-30T07:38:24.847 回答