我使用核心数据将商店存储为实体
商店,并且在每个商店上我都有一个属性
城市,我想按城市对商店进行分组并呈现UITableView
包含所有城市。我使用 NSFetchedResultsController 来获取数据并刷新 UITableView,因为我想对城市进行分组,所以我将请求的 resultType 设置为 NSDictionaryResultType
。
但是现在,当我在 NSFetchedResultsController
上使用 objectAtIndexPath
时,我得到 NSKnownkeysdictionary1
我的问题是我可以制作 NSFetchedResultsController
句柄吗? NSDictionaryResultType
或者我应该在这种情况下放弃使用 NSFetchedResultsController
并采取其他方法?
最佳答案
您必须为其设置 NSExpressionDescription 才能获取适当的值。你可以这样做,
- (NSFetchedResultsController *)fetchedResultsController
{
if (!_fetchedResultsController) {
NSExpression *cityKeypath = [NSExpression expressionForKeyPath:@"identifier"];
NSExpressionDescription *cityDescription = [[NSExpressionDescription alloc] init];
cityDescription.expression = cityKeypath;
cityDescription.name = @"City";
cityDescription.expressionResultType = NSStringAttributeType;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Shop"];
[fetchRequest setPropertiesToFetch:@[cityDescription]];
[fetchRequest setPropertiesToGroupBy:@[@"city"]];
[fetchRequest setResultType:NSDictionaryResultType];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"city" ascending:YES]];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
_fetchedResultsController.delegate = self;
NSError *error;
[_fetchedResultsController performFetch:&error];
}
return _fetchedResultsController;
}
因此,您需要为要获取的属性提供 NSExpressionDescription。 NSFetchedResultsController 结果将采用这样的字典类型,
{
@"city": "Kansas"
}
...
关于ios - NSFetchedResultsController 可以与 NSDictionaryResultType 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28171562/