1

我有以下方法,用于获取当天的所有事件:

- (NSArray *)fetchEventsForToday {

    NSDate *startDate = [NSDate date];

    // endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
    NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400];

    // Create the predicate. Pass it the default calendar.
    NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray]; 

    // Fetch all events that match the predicate.
    NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];

    NSLog(@"array: %@", events); 
    return events;
}

如果我检查我的 NSLog,我会在数组中得到以下对象:

2011-06-29 19:24:01.383 SimpleEKDemo[2945:207] array: (
    "EKEvent <0x5118cd0> { EKEvent <0x5118cd0>   {
        title = Test; 
        calendar = EKCalendar <0x5105d60> {
            title = Calendar; 
            type = Local; 
            account = (null); 
            allowsModify = YES; 
            color = 0.443137 0.101961 0.462745 1.000000
        }; 
        alarms = (null); 
        URL = (null); 
        lastModified = 2011-06-29 23:51:51 +0000
        }; 
        location = (null); 
        startDate = 2011-06-30 00:30:00 +0000; 
        endDate = 2011-06-30 01:30:00 +0000; 
        allDay = 0; 
        floating = 0; 
        recurrence = (null); 
        attendees = (null)
    }",

    "EKEvent <0x5118380> {EKEvent <0x5118380>   {
        title = Prueba; 
        calendar = EKCalendar <0x5105d60> {
            title = Calendar; 
            type = Local; 
            account = (null); 
            allowsModify = YES; 
            color = 0.443137 0.101961 0.462745 1.000000
        }; 
        alarms = (null); 
        URL = (null); 
        lastModified = 2011-06-29 23:51:58 +0000}; 
        location = (null); 
        startDate = 2011-06-30 00:30:00 +0000; 
        endDate = 2011-06-30 01:30:00 +0000; 
        allDay = 0; 
        floating = 0; 
        recurrence = (null); 
        attendees = (null)
    }",

    "EKEvent <0x5117f70> {EKEvent <0x5117f70>   {
        title = Numero; 
        calendar = EKCalendar <0x5105d60> {
            title = Calendar; 
            type = Local; 
            account = (null); 
            allowsModify = YES; 
            color = 0.443137 0.101961 0.462745 1.000000
        }; 
        alarms = (null); 
        URL = (null); 
        lastModified = 2011-06-29 23:53:54 +0000}; 
        location = (null); 
        startDate = 2011-06-30 00:30:00 +0000;
        endDate = 2011-06-30 01:30:00 +0000; 
        allDay = 0; 
        floating = 0; 
        recurrence = (null); 
        attendees = (null)
    }"

如您所见,我有 3 个对象:“test”、“Prueba”和“Numero”这个标题以及开始和结束日期是我需要的所有信息,但我不知道如何获取它。有人可以帮助我吗?

4

1 回答 1

1

你看过文档EKEvent吗?所需的属性是

  1. title
  2. startDate
  3. endDate

用法

NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
for ( EKEvent * event in events ) {
    NSLog(@"Title: %@, Start Date: %@, End Date: %@", event.title, event.startDate, event.endDate);
}
于 2011-06-30T02:23:55.843 回答