简体   繁体   中英

iOS Search Calendar using title property

I am trying to make an app that permits the user to make events in one specified calendar.

The problem is that:

I can't find a solution to know if there's a calendar with the title that I want to use.

If the list is empty I write a code that creates the calendar but if the list isn't empty I need to know if there's a calendar with the calendar.title that I need.

If there isn't any calendar, I create the calendar; if there is I add the event to this calendar.

Below is the code I am using:

EKEvent *myEvent;
EKEventStore *store;
EKSource* localSource;
EKCalendar* newCal;

store = [[EKEventStore alloc] init];
myEvent = [EKEvent eventWithEventStore: store];
NSString* title         = [arguments objectAtIndex:1];
NSString* location      = [arguments objectAtIndex:2];
NSString* message       = [arguments objectAtIndex:3];
NSString* startDate     = [arguments objectAtIndex:4];
NSString* endDate       = [arguments objectAtIndex:5];
NSString* calendarTitle = [arguments objectAtIndex:6];
//NSString* calID = nil;
//int i = 0;

EKCalendar* calendar = nil;
if(calendarTitle == nil){
    calendar = store.defaultCalendarForNewEvents;
} else {
    NSIndexSet* indexes = [store.calendars indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        *stop = false;
        EKCalendar* cal = (EKCalendar*)obj;
        if(cal.title == calendarTitle){
            *stop = true;
        }
        return *stop;
    }];

    if (indexes.count == 0) {
        //if list is empty i haven't calendars then i need to create it
        for (EKSource* source in store.sources)
        {
            if (source.sourceType == EKSourceTypeLocal)
            {
                localSource = source;
                break;
            }
        }

        if (!localSource) return;

        newCal = [EKCalendar calendarWithEventStore:store];
        calendar.source = localSource;
        calendar.title = calendarTitle;

        NSError* error;
        bool success = [store saveCalendar:newCal commit:YES error:&error];

        if (error != nil)

        {
            NSLog(error.description);
        }
        //calendar created

    } else {

        //!Empty List i need to search the calendar with the title = calendarTitle
        //And if there isn't i need to create it



        //calendar = [store.calendars objectAtIndex:[indexes firstIndex]];
    }
}

I think the problem is your implementation of indexesOfObjectsPassingTest. You're not returning any indexes, and since you try to stop it after it finds one index, you should just use the singular version indexOfObjectPassingTest. You can very simply write that like this:

     NSUInteger* indx = [store.calendars indexOfObjectPassingTest:^BOOL(EkCalendar *cal, NSUInteger idx, BOOL *stop) {
     return [cal.title isEqualToString:calendarTitle];
    }];

Then, after checking to see that index is not NSNotFound use

calendar = store.calendars[indx];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM