2

我正在尝试检查某个 nsdate 是否在我的 Event Store 中。当前月份的事件存储在一个NSMutableArray. 我正在使用这些函数来创建事件存储并在其上获取对象:

- (void) viewDidLoad{
[super viewDidLoad];

    self.almacenEventos = [EKEventStore new];
    self.listaEventos = [NSMutableArray new];

    self.calendario = [self.almacenEventos defaultCalendarForNewEvents];

    [self.listaEventos addObjectsFromArray:[self monthEvents]];

}

-(NSArray *) monthEvents{

    NSDate *firstMonthDay = [NSDate firstDayOfCurrentMonth];

    NSDate *lastMonthDay = [NSDate firstDayOfNextMonth];

    NSArray *calendarios = [[NSArray alloc]initWithObjects:self.calendario, nil];

    NSPredicate * predicado = [self.almacenEventos predicateForEventsWithStartDate:firstMonthDay endDate:lastMonthDay calendars:calendarios];

    return [self.almacenEventos eventsMatchingPredicate:predicado];
}

firstDayOfCurrentMonth并将firstDayOfNextMonth正确返回当前月份的日期。初始化后,self.listaEventos拥有所有当月事件。

检查 a 是否self.almacenEventos包含 a时会出现问题NSDate。我正在使用tapku 日历来显示月份网格。并遵循开发人员和其他第 3 方帮助的所有说明。

我已经调试了代码,试图找到问题所在。“d”是要比较的日期,返回长格式 2012-08-29 00:00:00 +0000。

单个事件的日期格式如下:

startDate = 2012-08-04 22:00:00 +0000;
endDate = 2012-08-05 21:59:59 +0000;

所以我被困在这个函数的中间,数组中存在一个给定日期的对象。如果我使用手动日期数组而不是事件数组,它就像一个魅力......那么我在这里做错了什么?是退回的NSDateComponents吗?我该怎么做?非常感谢

- (NSArray*) checkDate:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{

    NSMutableArray *marks = [NSMutableArray array];

    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSDateComponents *comp = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:startDate];

    NSDate *d = [NSDate new];
    d = [cal dateFromComponents:comp];

    // Init offset components to increment days in the loop by one each time
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:1];

    // for each date between start date and end date check if they exist in the data array
    while (YES) {      

        // Is the date beyond the last date? If so, exit the loop.
        // NSOrderedDescending = the left value is greater than the right
        if ([d compare:lastDate] == NSOrderedDescending) {
            break;
        }

        // If the date is in the data array, add it to the marks array, else don't
        if ([self.listaEventos containsObject:d]) {
            [marks addObject:[NSNumber numberWithBool:YES]];
        } else {
            [marks addObject:[NSNumber numberWithBool:NO]];
        }

        // Increment day using offset components (ie, 1 day in this instance)
        d = [cal dateByAddingComponents:offsetComponents toDate:d options:0];
    }

    return [NSArray arrayWithArray:marks];

}
4

1 回答 1

1

checkDate:marksFromDate:toDate方法期望self.listaEventosNSMutableDictionary包含以下数组的日期索引EKEvent

// There are events on these 4 dates
self.listaEventos = {
    "2012-08-15 00:00:00 +0000" = (...),
    "2012-08-17 00:00:00 +0000" = (...),
    "2012-08-18 00:00:00 +0000" = (...),
    "2012-08-29 00:00:00 +0000" = (...)
}

我会改为monthEvents

-(NSMutableDictionary *) monthEvents{
    NSMutableDictionary *retVal;

    NSDate *firstMonthDay = [NSDate firstDayOfCurrentMonth];
    NSDate *lastMonthDay = [NSDate firstDayOfNextMonth];
    NSArray *calendarios = [[NSArray alloc]initWithObjects:self.calendario, nil];
    NSPredicate * predicado = [self.almacenEventos predicateForEventsWithStartDate:firstMonthDay endDate:lastMonthDay calendars:calendarios];

    NSArray *results = [self.almacenEventos eventsMatchingPredicate:predicado];
    // Next add all events to our dictionary indexed by date
    for (EKEvent *evt in events) {
        NSDate *dateKey = [self dateWithoutTime:evt];

        // A date may have multiple events. Get all current events for the date
        NSArray *eventsForDate = [retVal objectForKey:dateKey];
        // If no events were set for the date
        if (![eventsForDate isKindOfClass:[NSArray class]]) {
            // create an array using a factory method
            eventsForDate = [NSArray arrayWithObject:evt];
        } else {
            // append the object to our array of events
            eventsForDate = [eventsForDate arrayByAddingObject:evt];
        }

        // Update the dictionary
        [retVal setObject:eventsForDate forKey:dateKey];
    }

    return retVal;
}

- (NSDate *)dateWithoutTime:(NSDate *)date 
{
    TKDateInformation info = [date dateInformationWithTimeZone:[NSTimeZone systemTimeZone]];
    // lop off hour/minute/seconds so all dates for the day are equal
    info.hour = 0;
    info.minute = 0;
    info.second = 0;

    return [NSDate dateFromDateInformation:info timeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
}
于 2012-08-15T17:56:17.517 回答