gpt4 book ai didi

objective-c - iOS如何通过一个循环将多个事件添加到日历

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:45 25 4
gpt4 key购买 nike

我知道下面的代码对于创建事件并将事件添加到 iOS 日历非常有用。它工作得很好,我可以添加很多事件,但对于一个事件,我必须在按钮上按一下。

- (IBAction)add_event:(id)sender{     
EKEventStore *eventStore=[[EKEventStore alloc] init];
EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
addEvent.title=@"title";
addEvent.startDate = [NSDate date];
addEvent.endDate = [addEvent.startDate dateByAddingTimeInterval:600];
[addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err];
if (err == nil) {
NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier];
NSLog(@"Event ID: %@" , str );
}
else {
NSLog(@"Error %@",err);
}
}

然后我尝试在按钮触摸事件上添加许多带有 while 循环的事件,我在此处编辑的代码:

- (IBAction)add_event:(id)sender{
int i = 0;
while(i < 100){
EKEventStore *eventStore=[[EKEventStore alloc] init];
EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
addEvent.title=@"title";
addEvent.startDate = [NSDate date];
addEvent.endDate = [addEvent.startDate dateByAddingTimeInterval:600];
[addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err];
if (err == nil) {
NSString* str = [[NSString alloc] initWithFormat:@"%@", addEvent.eventIdentifier];
NSLog(@"Event ID %d: %@",i, str);
}
else {
NSLog(@"Error %@",err);
}
i++;
}
}

我想在日历中添加 100 个事件,但只有 82 个事件添加成功,83 号的所有事件都出现了一些错误。这是我的日志屏幕:

.......

2013-03-07 15:08:07.742 MyDTUSchedule[3066:c07] Event ID 79: BBCF7782-5D60-42D7-8478-EF80604FBF41:B0124DEE-EC5F-40B9-B9F8-312FA07D8059
2013-03-07 15:08:07.756 MyDTUSchedule[3066:c07] Event ID 80: BBCF7782-5D60-42D7-8478-EF80604FBF41:613F794D-67BE-4704-BEC2-7439E77965F0
2013-03-07 15:08:07.781 MyDTUSchedule[3066:c07] Event ID 81: BBCF7782-5D60-42D7-8478-EF80604FBF41:2FEF3B6D-6AC0-4058-AA79-BB46FEBDF732
2013-03-07 15:08:07.810 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6a380 {NSLocalizedDescription=No calendar has been set.}
2013-03-07 15:08:07.812 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6a9d0 {NSLocalizedDescription=No calendar has been set.}
2013-03-07 15:08:07.813 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6c310 {NSLocalizedDescription=No calendar has been set.}
2013-03-07 15:08:07.815 MyDTUSchedule[3066:c07] Error Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x7e6d5d0 {NSLocalizedDescription=No calendar has been set.}

...........

你能帮我解决这个问题吗?我在 iOS Simulator 5.0 中运行这段代码。 iOS 模拟器 6.0 出现相同的错误,但事件编号为 124

最佳答案

这可能是内存问题。 eventStore 在每次循环迭代时分配并且永远不会释放。

尝试将 EKEventStore *eventStore=[[EKEventStore alloc] init]; 从循环中取出。

关于objective-c - iOS如何通过一个循环将多个事件添加到日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15285014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com