0

我希望只显示核心数据获取请求的唯一结果。目前从研究中我看到可以通过使用来实现这一点,NSDictionaryResultType但我一直在努力让它发挥作用。

我确实尝试使用以下内容,但无法将其正确集成到我的班级中。我不是 100% 确定要放什么,NSArray *distincResults因为它出现了未使用的变量:

     NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routines" inManagedObjectContext:managedObjectContext];
 request.entity = entity;
 request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"routinename"]];
 request.returnsDistinctResults = YES;
 request.resultType = NSDictionaryResultType;

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"routinename" ascending:YES];
 [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

 NSError *error = nil;
 NSArray *distincResults = [managedObjectContext executeFetchRequest:request error:&error];
 // Use the results

有什么建议么?

4

1 回答 1

0

如果使用NSDictionaryResultType,则不能使用 FRC 委托来监视更改。如果没问题,你可以沿着这条路线走。

一旦你有了一个字典数组 ( distinctResults) ,就让它成为你的表视图的数据数组。因此,例如,在cellForRowAtIndexPathor中configureCell,使用

cell.textLabel.text = distinctResults[indexPath.row][@"routinename"];

这是

cell.textLabel.text = [[distinctResults objectAtIndex:indexPath.row] 
                          objectForKey:@"routinename"];
于 2013-07-16T15:01:33.663 回答