2014-01-16 66 views
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]; 

         }]; 

}

回答

1

試試這個:

NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init]; 
oneDayAgoComponents.day = -1; 
+0

謝謝!沒有注意到這個錯字。多麼愚蠢的錯誤......好看的:* –

+0

@MichalShatz樂趣:-)這是一個「及時」的問題 - 就在我喝了一杯濃咖啡之後。 – Unheilig

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; 
          }); 

         }]; 

}

相關問題