2014-08-30 61 views
0

我想按周和日期对核心数据条目进行分组。我有一个名为WorkEntry的核心数据实体,其中的属性是weekBegindate。在这些我保存它保存的日期以及与该日期相对应的一周的第一天。我使用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

我会任何帮助你们能给真的很感激。试图弄清楚这一点我一直在疯狂。由于

+1

您的提取请求告诉Core Data将每个结果作为NSDictionary返回。很可能某个地方你试图访问一个结果,就好像它是一个NSString,当它是一个NSDictionary的时候 - 可能读取一个属性值(也就是说你假设它返回了一些属性作为结果的每一项) - 可能'weekBegin'。 – quellish 2014-08-31 01:03:01

回答

0

问题可能是你在你控制器声明distinctResults财产的方式。 如果你使用弱指针,它可能会被释放。

+0

我只是改变了它,它仍然崩溃 – nikarc 2014-08-30 23:01:28