我在core data
中有一列存儲日期(eg:2006-01-26)
。我使用了一個數組來存儲所有年份,在這些存儲日期上使用NSDateComponents
(例如:如果我使用NSDateComponents
,那麼我將2006年作爲一年)。這些年份是NSNumbers
,並且在我的謂詞中使用它們,我想要獲取包含所有Date組件的列NSDate
。提取的記錄應該只有特定的一年。我現在對如何將一年的組件與一個數組匹配並創建謂詞感到困惑。如何使用NSPredicate獲取具有NSDate類型的屬性?
- (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];
}
一旦我得到這個,我可以顯示紀錄N個我的viewController。
一些代碼請..... – iamsult 2012-04-04 05:15:46
我在這裏是新的。你可以告訴我如何附加代碼? – PriyankaJ 2012-04-04 05:41:24
您可以將您的代碼複製並粘貼到您的問題中。然後突出顯示您的代碼並點擊{}按鈕。它位於您輸入問題的文本框的正上方。這樣做會縮進突出顯示的區域並顯示代碼。 – Linger 2012-04-04 15:55:01