将两个NSArray与不可比较的对象相交

时间:2015-02-20 17:20:09

标签: ios objective-c eventkit

以下代码从EventKit中获取所选日期的事件数组,然后使用空事件“填充”现有日历事件,以填充整天的每小时时段(例如,9-5)。

来自iOS日历的事件包含在我创建的自定义事件容器类中。事件类具有以下属性:

// HDEvent.h
@interface HDEvent : NSObject

@property(nonatomic, copy) NSDate *startDate;
@property(nonatomic, copy) NSDate *endDate;
@property(nonatomic, strong) EKEvent *event;
@property(nonatomic, copy) NSString *eventIdentifier;
@property(nonatomic, copy) NSString *tempTitle;

@end

我试图根据HDEvent对象的开始/结束日期,用日历中的任何真实事件替换空事件。以下方法的传入数组包含设备日历中的事件(如果有)。

- (NSArray *)addEmptyEventsWithEvents:(NSMutableArray *)events
                              ForDate:(NSDate *)date {

    NSMutableOrderedSet *finalEvents = [[NSMutableOrderedSet alloc] init];

    // this returns an array filled with the empty HDEvents
    // for the expected date range.
    NSArray *emptyEvents = [self getEventsTimeRangeFromDate:date];

    for (HDEvent *emptyEvent in emptyEvents) {
        HDEvent *eventToAdd;
        for (HDEvent *event in events) {
            NSLog(@"Event: %@", event.event.title);
            if ([event.startDate isEqualToDate:emptyEvent.startDate] ||
                [event.endDate isEqualToDate:emptyEvent.endDate])
            {
                eventToAdd = event;
            } else {
                eventToAdd = emptyEvent;
            }
        }
        [finalEvents addObject:eventToAdd];
    }
    return [finalEvents array];
}

我正在尝试使用我的事件创建一个数组,如下面的伪代码所示:

// Calendar Events
[
    10:00-11:00 MEETING,
    11:00-12:00 LUNCH
]

// Empty Placeholders
[
    09:00-10:00 EMPTY,
    10:00-11:00 EMPTY,
    11:00-12:00 EMPTY,
    12:00-01:00 EMPTY,
    01:00-02:00 EMPTY,
    02:00-03:00 EMPTY,
    03:00-04:00 EMPTY,
    04:00-05:00 EMPTY,
]

// Final desired result:
[
    09:00-10:00 EMPTY,
    10:00-11:00 MEETING,
    11:00-12:00 LUNCH,
    12:00-01:00 EMPTY,
    01:00-02:00 EMPTY,
    02:00-03:00 EMPTY,
    03:00-04:00 EMPTY,
    04:00-05:00 EMPTY,
]

[问题]

问题是返回的数组只包含日历中的一个事件(虽然我可以在NSLog响应中看到我当天有两个事件)

如果我交换for循环,那么我会得到9个事件(同样,只有一个来自日历,还有一个重复的空事件。)

1 个答案:

答案 0 :(得分:0)

谢谢@vikingosegundo。

我最后在本课程中按照此答案中包含的建议实施了自定义比较方法:https://stackoverflow.com/a/877881/810360

我还通过使用GitHub上发现的Matthew York的优秀DateTools将其扩展为更灵活:https://github.com/MatthewYork/DateTools

这是我的最终代码:

-(BOOL)isEqualToEvent:(HDEvent*)event {

    if (self == event) {
        return YES;
    }

    if ([event.startDate isEqualToDate:self.startDate] &&
        [event.endDate isEqualToDate:self.endDate]) {
        return YES;
    } else {
        return NO;
    }

}

-(BOOL)doesEventIntersectEvent:(HDEvent*)event {
    DTTimePeriod *eventTimePeriod = [DTTimePeriod timePeriodWithStartDate:event.startDate endDate:event.endDate];
    DTTimePeriod *selfTimePeriod = [DTTimePeriod timePeriodWithStartDate:self.startDate endDate:self.endDate];
    if ([eventTimePeriod intersects:selfTimePeriod]) {
        return YES;
    } else {
        return NO;
    }
}

-(BOOL)doesEventContainEvent:(HDEvent*)event {
    DTTimePeriod *eventTimePeriod = [DTTimePeriod timePeriodWithStartDate:event.startDate endDate:event.endDate];
    DTTimePeriod *selfTimePeriod = [DTTimePeriod timePeriodWithStartDate:self.startDate endDate:self.endDate];
    if ([eventTimePeriod contains:selfTimePeriod]) {
        return YES;
    } else {
        return NO;
    }
}