title: Trying To Group Core Data Fetch Results By Date
body: <p>I want to group core data entries by week and date. I have a core data entity named <code>WorkEntry</code> which among it's attributes I have <code>weekBegin</code> and <code>date</code>. In these I save the date it was saved on as well as the first day of the week corresponding to that date. I use <code>weekBegin</code> for the section name and I want the cells to show the dates that have entries with no duplicates. I used <a href="http://felipecypriano.com/2011/09/21/core-data-how-to-do-a-select-distinct/" rel="nofollow noreferrer">http://felipecypriano.com/2011/09/21/core-data-how-to-do-a-select-distinct/</a> as a guide. Here's my code:</p>
  <p>Fetch Request with <code>returnsDistinctResults</code>:</p>
  <pre><code>- (NSFetchRequest *)workDayFetchRequest {
      NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@&quot;WorkEntry&quot;];
      CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
  
      NSEntityDescription *entity = [NSEntityDescription entityForName:@&quot;WorkEntry&quot; inManagedObjectContext:coreDataStack.managedObjectContext];
      fetchRequest.entity = entity;
      fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@&quot;weekBegin&quot;]];
      fetchRequest.returnsDistinctResults = YES;
      fetchRequest.resultType = NSDictionaryResultType;
  
      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@&quot;weekBegin&quot; ascending:YES];
      [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
  
      NSError *error = nil;
      self.distinctResults = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:&amp;error];
  
      return fetchRequest;
  }
  </code></pre>
  <p>which gives me an array called <code>distinctResults</code> which I use in:</p>
  <p><code>CellForRowAtIndexPath</code>:</p>
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @&quot;Cell&quot;;
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  
      cell.textLabel.text = [self.distinctResults objectAtIndex:indexPath.row];
  
      return cell;
  }
  </code></pre>
  <p>But I have a feeling that may not be right. When I switch to the table view in the app it crashes with this exception:</p>
  <p><code>-[NSKnownKeysDictionary1 length]: unrecognized selector sent to instance 0x8c7c8a0</code></p>
  <p>I'd be really grateful for any help you guys can give. I've been going crazy trying to figure this out. Thanks</p>