IOS/xcode/coredata: 使用核心数据实现日历控件

IOS/xcode/coredata: Implement calendar control using core data

IOS newb 在这里尝试在我的项目中使用存储在核心数据中的数据实现一个开源日历控件 MADay。日历控件不提供访问核心数据的文档。

我猜我需要在从对应于下面 "Events" 数组的核心数据中提取事件后创建一个 NSArray。这就是全部?如果是这样,我该怎么做?

感谢您的任何建议。

这是库中的数据源代码:

- (NSDate *)nextDayForDate:(NSDate *)date;
- (NSArray *)eventKitEventsForDate:(NSDate *)date;
- (NSArray *)eventKitEventsToMAEvents:(NSArray *)eventKitEvents;

@property (readonly) EKEventStore *eventStore;

@end

@implementation MAEventKitDataSource


- (NSArray *)dayView:(MADayView *)dayView eventsForDate:(NSDate *)date
{
    return [self eventKitEventsToMAEvents:[self eventKitEventsForDate:date]];
}

- (NSArray *)weekView:(MAWeekView *)weekView eventsForDate:(NSDate *)date
{
    return [self eventKitEventsToMAEvents:[self eventKitEventsForDate:date]];
}


- (EKEventStore *)eventStore
{
    if (!_eventStore) {
        _eventStore = [[EKEventStore alloc] init];
    }
    return _eventStore;
}


- (NSDate *)nextDayForDate:(NSDate *)date
{
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:1];

    return [CURRENT_CALENDAR dateByAddingComponents:components toDate:date options:0];
}

- (NSArray *)eventKitEventsForDate:(NSDate *)startDate
{
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
                                                                      endDate:[self nextDayForDate:startDate]
                                                                    calendars:nil];

    NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
    return events;
}

- (NSArray *)eventKitEventsToMAEvents:(NSArray *)eventKitEvents
{
    NSMutableArray *events = [[NSMutableArray alloc] init];
    for (EKEvent *event in eventKitEvents) {
        MAEvent *maEvent = [[MAEvent alloc] init];
        maEvent.title  = event.title;
        maEvent.start  = event.startDate;
        maEvent.end    = event.endDate;
        maEvent.allDay = event.allDay;

        maEvent.backgroundColor = [UIColor colorWithCGColor:event.calendar.CGColor];
        maEvent.textColor       = [UIColor whiteColor];

        [events addObject:maEvent];
    }
    return events;
}

我自己没有使用过这个框架,但据我从代码和 EventKit 文档中看到的,它有自己的存储,因此不需要进一步的核心数据处理.

https://developer.apple.com/library/prerelease/ios/documentation/EventKit/Reference/EKEventStoreClassRef/index.html#//apple_ref/occ/instm/EKEventStore/saveCalendar:commit:error:

所以:阅读文档理解;)