【问题标题】:Check if NSDate is within an EKEventStore检查 NSDate 是否在 EKEventStore 中
【发布时间】:2012-08-05 09:47:14
【问题描述】:

我正在尝试检查某个 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];
}

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

检查self.almacenEventos 是否包含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];

}

【问题讨论】:

    标签: ios ios5 eventkit nscalendar tapku


    【解决方案1】:

    checkDate:marksFromDate:toDate 方法期望 self.listaEventos 是一个由包含 EKEvent 数组的日期索引的 NSMutableDictionary

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

    【讨论】:

    • 感谢 Jon 的方法,确实非常聪明。我最终决定使用一个实体来存储 EKEvents 以及日期、警报等附加信息。因此,我在 NSArray 中而不是在日历中查找日期更容易。我还实现了一个类似的“dateWithoutTime”函数,它删除了小时、分钟和秒。我会在需要时将您的代码作为 NSMutableDictionary 看一下,酷乔恩,再次感谢
    • @sam80 我很想看看你的最终代码。我从不喜欢按日期(没有时间)索引所有事件,但这正是 Tapku 演示所期望的。
    猜你喜欢
    • 2016-12-22
    • 2016-05-21
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2011-04-19
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多