3

以下のコードは、イベントを作成して iOS カレンダーに追加するのに非常に便利です。それはうまく機能し、多くのイベントを追加できますが、1 つのイベントでボタンを 1 回タッチする必要があります。

- (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 です。

4

1 に答える 1

2

メモリの問題かもしれません。eventStoreループの反復ごとに割り当てられ、解放されることはありません。

EKEventStore *eventStore=[[EKEventStore alloc] init];ループから出してみる。

于 2013-03-08T01:45:05.827 に答える