我收到一个错误:
删除事件错误: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];
}
最佳答案
发生这种情况是因为您总是初始化 EKEventStore 的新实例
。当您将 EKEvent
添加到 EKEventStore
时,EKEventStore
的实例与您尝试删除时不同。您可以做的是在 .h 中声明 EKEventStore
引用变量并仅初始化一次。
在 .h -
EKEventStore *eventStore;
在.m-
在 viewDidLoad
中 -
eventStore = [[EKEventStore alloc] init];
然后从所有三种方法中删除这一行-
EKEventStore *eventStore = [[EKEventStore alloc] init];
关于iphone - EKEventStore removeEvent EKErrorDomain 代码=11 EKErrorObjectBelongsToDifferentStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10811726/