1
所以我需要獲取日曆中的當前事件。 I.E--一個已經開始但尚未結束的事件。我寫了一些代碼,但它不起作用。 通過調試,我發現我的oneDayAgo變量是零,我不明白爲什麼。 oneWeekFromNow變量很好。EKEvent獲取當前事件iOS7
這是我寫的方法:
-(void)getCurrentEvent{
// Get appropriate calendar
[self.store requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day -=1;
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date]
options:0];
NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
oneWeekFromNowComponents.week = 1;
NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
toDate:[NSDate date]
options:0];
NSPredicate *predicate = [self.store predicateForEventsWithStartDate:oneDayAgo
endDate:oneWeekFromNow
calendars:nil];
NSMutableArray *currentEvens = [[NSMutableArray alloc]init];
// Fetch all events that match the predicate
[self.store enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
if (([event.startDate compare:[NSDate date]] == NSOrderedDescending) &&
([[NSDate date] compare:event.endDate] == NSOrderedDescending)) {
[currentEvens addObject:event];
}
}];
self.lblEvent.text = [NSString stringWithFormat:@"%@", currentEvens];
[self.view reloadInputViews];
}];
}
謝謝!沒有注意到這個錯字。多麼愚蠢的錯誤......好看的:* –
@MichalShatz樂趣:-)這是一個「及時」的問題 - 就在我喝了一杯濃咖啡之後。 – Unheilig