2014-01-16 5 views
1

그래서 캘린더에서 현재 이벤트를 가져와야합니다. I.E - 아직 시작하지 못했던 이벤트. 일부 코드를 작성했지만 작동하지 않습니다. 디버깅을 통해 oneDayAgo 변수가 0 인 것으로 파악되어 그 이유를 알 수 없습니다. 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; 
          }); 

         }]; 

}