0

我正在尝试制作一个允许用户在一个指定的日历中进行事件的应用程序。

问题是:

我找不到一个解决方案来知道是否有一个带有我想使用的标题的日历。

如果列表为空,我编写一个创建日历的代码,但如果列表不为空,我需要知道是否有我需要的日历calendar.title

如果没有日历,我创建日历;如果有,我将事件添加到此日历。

下面是我正在使用的代码:

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]];
    }
}
4

1 回答 1

0

我认为问题是您对indexesOfObjectsPassingTest 的实现。您没有返回任何索引,并且由于您在找到一个索引后尝试停止它,因此您应该只使用单数版本 indexOfObjectPassingTest。你可以很简单地写成这样:

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

然后,检查后发现索引不是 NSNotFound 使用

calendar = store.calendars[indx];
于 2013-01-29T18:55:49.437 回答