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