2012-11-09 149 views
0

我有一個Event表保存NSString *actionNSDate *date。我想過濾從核心數據檢索的數組,以顯示在最後一小時和下一小時執行的所有操作,而不取決於它保存的那一天。星期二的例子在16:00節省了一個動作,但是今天是星期五17:00,我仍然想要檢索星期二保存的動作以及該時間範圍內的任何動作。使用核心數據與NSPredicate和NSDate

回答

0

對於任何卡住這個問題的人。更改我的事件表以保存一個整數(小時的動作)並像這樣檢索它

NSDate *currentFullDate = [NSDate date]; 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:currentFullDate]; 
NSInteger currentHour = [components hour]; 
NSInteger hourAgo = currentHour -1; 
NSInteger nextHour = currentHour +1; 


NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Event"]; 
request.predicate = [NSPredicate predicateWithFormat:@"(hour >=%@) AND (hour <= %@)", hourAgo, nextHour]; 
request.fetchLimit = 5; 
NSError *error; 
NSArray *eventArray = [managedObjectContext executeFetchRequest:request error:&error]; 

for (Event *event in eventArray) 
{ 

}