0

新しいカレンダーを作成し、そのカレンダーを使用してイベントなどを追加するアプリを作成しています。カレンダーは問題なく作成できますが、カレンダーが存在するかどうかを確認するためにチェックを実行しようとしています。毎回同じ名前で 2 番目のものを作成しないでください。つまり、新しいカレンダーは 1 回だけ作成します。

int 変数を設定し、ループを実行してデバイス上の各カレンダーのタイトル プロパティを確認していますが、検索しているカレンダー名の文字列が一致しても、int 変数は変更されません。

「カレンダーのチェック」コードについて私が持っているものは次のとおりです。

-(void)checkForCalendar {

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    NSArray *calendarArray = [eventStore calendarsForEntityType:EKEntityTypeEvent];
    //NSLog(@"%@", calendarArray);

    for (int x = 0; x < [calendarArray count]; x++) {

        EKCalendar *cal = [calendarArray objectAtIndex:x];
        NSString *title = [cal title];
        if ([title isEqualToString:@"AFTP"] ) {
            calendarExists = 1;
        }else{
            calendarExists = 0;
        }
    }

[self createCalendar];

}

そして、ここに私が「作成」カレンダー部分のために持っているものがあります:(これはうまくいきます.intの1ではなく常に「0」を取得していcalendarExistsます.

-(void)createCalendar {

    NSLog(@"%d",calendarExists);
    if (calendarExists == 0) {
        EKEventStore* eventStore = [[EKEventStore alloc] init];

        NSString* calendarName = @"AFTP";
        EKCalendar* calendar;

        // Get the calendar source
        EKSource* localSource;
        for (EKSource* source in eventStore.sources) {
            if (source.sourceType == EKSourceTypeCalDAV)
            {
                localSource = source;
                break;
            }
        }

        if (!localSource)
            return;

        calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
        calendar.source = localSource;
        calendar.title = calendarName;

        [eventStore saveCalendar:calendar commit:YES error:nil];
    }
}
4

1 に答える 1

1

私はあなたのコードのこのセクションで考えます:

    if ([title isEqualToString:@"AFTP"] ) {
        calendarExists = 1;
    }else{
        calendarExists = 0;
    }

変数を 1 に設定した後、ブレークする必要があります。そうしないと、ループの次のターンで再び 0 に設定されます。

    if ([title isEqualToString:@"AFTP"] ) {
        calendarExists = 1;
        break;
    }
于 2012-10-21T20:44:26.300 に答える