1

所以我需要在日历中获取当前事件。IE - 一个已经开始但尚未结束的事件。我写了一些代码,但它不起作用。通过调试,我注意到我的 oneDayAgo 变量为零,我不明白为什么。oneWeekFromNow 变量很好。

这是我写的方法:

-(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];

                      }];

}

4

2 回答 2

1

试试这个:

NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;
于 2014-01-16T21:25:44.223 回答
0

这是对我有用的更正代码。我还需要修改一些其他的东西:

-(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]] == NSOrderedAscending) &&
                                  ([[NSDate date] compare:event.endDate] == NSOrderedAscending)) {
                                  [currentEvens addObject:event];
                              }

                          }];

                          NSString *currentEventsString = [[NSString alloc]init];
                          for (EKEvent *event in currentEvens) {
                              currentEventsString = [currentEventsString stringByAppendingString:event.title];
                          }

                          dispatch_async(dispatch_get_main_queue(), ^{
                              self.lblEvent.text = currentEventsString;
                          });

                      }];

}

于 2014-01-17T08:38:33.890 回答