Ios NSFetchedResultsController能否与NSDictionaryResultType一起工作?
我使用核心数据将店铺存储为Ios NSFetchedResultsController能否与NSDictionaryResultType一起工作?,ios,cocoa,core-data,nsfetchedresultscontroller,Ios,Cocoa,Core Data,Nsfetchedresultscontroller,我使用核心数据将店铺存储为实体店铺,并且在我拥有属性城市的每个店铺上,我希望按城市对店铺进行分组,并显示一个包含所有城市的UITableView。我使用NSFetchedResultsController获取数据并刷新UITableView,因为我想对城市进行分组,所以我将请求的resultType设置为NSDictionaryResultType。 但是现在,当我在我的NSFetchedResultsController上使用objectAtIndexPath时,我得到的是NSKnownkey
实体
店铺,并且在我拥有属性
城市的每个店铺上,我希望按城市对店铺进行分组,并显示一个包含所有城市的UITableView
。我使用NSFetchedResultsController
获取数据并刷新UITableView
,因为我想对城市进行分组,所以我将请求的resultType
设置为NSDictionaryResultType
。
但是现在,当我在我的
NSFetchedResultsController上使用objectAtIndexPath
时,我得到的是NSKnownkeysdictionary1
我的问题是,我能让NSFetchedResultsController
处理nsDictionaryResultsType
吗?在这种情况下,我应该停止使用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;
}
{
@"city": "Kansas"
}
...
因此,需要为要获取的属性提供NSExpressionDescription。NSFetchedResultsController结果将在字典中显示如下类型:
- (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;
}
{
@"city": "Kansas"
}
...
它似乎会起作用,但如何访问cellforrowatinexpath
的字典?似乎objectAtIndexPath
和fetchedObjects
fromNSFetchedResultsController
都不能真正做到这一点。fetchedObjects应该为您提供字典数组。