我有一列core data
存储日期(eg:2006-01-26)
。我使用数组来存储所有年份,方法是使用NSDateComponents
这些存储日期。(例如:如果我使用上述日期NSDateComponents
,我将 2006 年作为一年)。这些年是NSNumbers
,并且在我的谓词中使用它们我想获取NSDate
包含所有 Date 组件的列。并且获取的记录应该只是特定年份的。我现在对如何将年份组件与数组中的一个组件匹配并创建谓词感到困惑。
- (void)viewDidLoad
{
[super viewDidLoad];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSError *error;
//AppDelegate *app = [[UIApplication sharedApplication]delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movies" inManagedObjectContext:self.managedObjectContxt];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"releaseDate" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
[request setSortDescriptors:sortDescriptors];
//[request setReturnsDistinctResults:YES ];
// [request setEntity:entity];
[sortDescriptors release];
[sortDescriptor release];
[request setPropertiesToFetch:[NSArray arrayWithObject:@"releaseDate"]];
NSMutableArray *arrOfdMovies = [[self.managedObjectContxt executeFetchRequest:request error:&error]mutableCopy];
[self setArrOfFetchdMovies:arrOfdMovies];
arrOfDistinctYr = [[NSMutableArray alloc]init];
NSMutableArray *temp = [[NSMutableArray alloc]init];
for (int i = 0; i<[arrOfdMovies count]; i++) {
// NSLog(@"%@",[[arrOfdMovies objectAtIndex:i] valueForKey:@"releaseDate"]);
//date to its components
NSDate *now = [[arrOfdMovies objectAtIndex:i]valueForKey:@"releaseDate"];
// Specify which units we would like to use
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *component_s = [calendar components:units fromDate:now];
NSInteger year = [component_s year];
NSLog(@"%i", year);
// NSInteger month = [components month];
// NSInteger day = [components day];
NSNumber *num= [NSNumber numberWithInteger:year];
[temp addObject:num];
}
[self setArrOfDistinctYr:temp];
//distinct value logic.
NSSet *uniqueYrs = [NSSet setWithArray:arrOfDistinctYr];
arr = [[NSArray alloc]initWithArray:[uniqueYrs allObjects]];
//sorting
NSArray *ar = [arr sortedArrayUsingSelector:@selector(compare:)];
self.arr = ar;
[self.managedObjectContxt retain];
[request release];
}
数组“ar”仅包含不同且有序的年份。但是这个数组元素的数据类型是“NSNumbr”现在我如何形成一个谓词,它将利用这个数组来显示特定年份的记录(如何从核心数据的日期字段中检查年份[日期为 2006 -01-26])。我尝试了以下方式 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return 2;
NSFetchRequest *requst=[[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movies" inManagedObjectContext:self.managedObjectContxt];
[requst setEntity:entity];
requst.predicate = [NSPredicate predicateWithFormat:@"releaseDate CONTAINS%@", [arr objectAtIndex:section]];
NSError *error = nil;
NSMutableArray *array = [[self.managedObjectContxt executeFetchRequest:requst error:&error]mutableCopy];
[requst release];
return [array count];
}
一旦我得到这个,我可以在我的 viewController 中显示记录。