嗨,我是这个 coredata 的新手。我有两个实体 CategoryList 和 ExpenseList。CategoryList 包含两个属性
1.dates (nsdate)
2.categories (nsstring)
并在与 ExpenseList 的许多关系中,关系名称为“expenseList” ExpenseList 由 1.amountSpent (float) 组成。
我只是想了解 coredata 中的关系特征。当我尝试创建一个 nspredicate 以按特定日期过滤数据时,它什么也不返回。我只是想过滤 CategoryList 实体中特定日期的类别属性内容。这是我的代码
-(void) callingByDate
{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSEntityDescription *categoryL = [NSEntityDescription entityForName:@"CategoryList" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *dateSort = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:dateSort, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dates == %@",date];
NSFetchRequest *dateFetch = [[NSFetchRequest alloc]init];
[dateFetch setSortDescriptors:sortDescriptors];
[dateFetch setEntity:categoryL];
[dateFetch setPredicate:predicate];
NSMutableArray *results = [[managedObjectContext executeFetchRequest:dateFetch error:nil] mutableCopy];
if([results count] > 0)
NSLog(@"results found");
else
NSLog(@"no results found");
}
当我在 nslog 区域中执行此代码时,它显示“未找到结果”。我不知道为什么。提前致谢
这是我找到一天的结束和开始的代码
-(NSDate *)beginningOfDay:(NSDate *)date;
{
NSDate *dat = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dat];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date;
{
NSDate *dat = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dat];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
当我 nslog 时,显示的结果是 startday -2013-04-30 18:30:00 +0000 endOfDay -2013-05-01 18:29:59 +0000