[英]Trying To Group Core Data Fetch Results By Date
我想按星期和日期对核心数据条目进行分组。 我有一个名为WorkEntry
的核心数据实体,在它的属性中,我具有weekBegin
和date
。 在这些文件中,我保存了保存日期以及该日期对应的一周的第一天。 我将weekBegin
用作节名,并且希望单元格显示具有重复项的日期。 我使用http://felipecypriano.com/2011/09/21/core-data-how-to-do-a-select-distinct/作为指南。 这是我的代码:
取得带有returnsDistinctResults
请求:
- (NSFetchRequest *)workDayFetchRequest {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkEntry"];
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WorkEntry" inManagedObjectContext:coreDataStack.managedObjectContext];
fetchRequest.entity = entity;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"weekBegin"]];
fetchRequest.returnsDistinctResults = YES;
fetchRequest.resultType = NSDictionaryResultType;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"weekBegin" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error = nil;
self.distinctResults = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:&error];
return fetchRequest;
}
这给了我一个叫做distinctResults
的数组,该数组用于:
CellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.distinctResults objectAtIndex:indexPath.row];
return cell;
}
但是我有一种不正确的感觉。 当我在应用程序中切换到表格视图时,它因以下异常而崩溃:
-[NSKnownKeysDictionary1 length]: unrecognized selector sent to instance 0x8c7c8a0
非常感谢你们能提供的任何帮助。 我一直在疯狂试图解决这个问题。 谢谢
问题可能出在您在控制器中声明distinctResults属性的方式。 如果使用弱指针,它可能会被释放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.