ios - 检查 NSDate 是否在 EKEventStore 中

标签 ios ios5 eventkit nscalendar tapku

我正在尝试检查某个 nsdate 是否在我的事件存储中。当前月份的事件存储在 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];

}

最佳答案

checkDate:marksFromDate:toDate 方法期望 self.listaEventos 是一个 NSMutableDictionary,由包含 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]];
}

关于ios - 检查 NSDate 是否在 EKEventStore 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815442/

相关文章:

ios - 将 NSData 与字节序列进行比较?

android - 带有设备自定义模型的 Django 推送通知

ios - 在对象数组的字典中应用过滤器

javascript - iOS 上的 Photoswipe 图库关闭事件

iphone - 如何在 iOS 中播放从网络接收的视频流?

iphone - 在 ios 6 中使用 EventKit 创建新日历时出现 "calendar has no source"错误

cocoa - OSX 10.8 : Where is eventkit. h?

ios - Swift 中的 CFNotificationCenterAddObserver

iphone - 从 UITableView 选择单元格并转到另一个 UITableView

ios - FSCalender 事件在 swift 中显示