NSFetchedResultsController 可以与 NSDictionaryResultType 一起使用吗?
Can NSFetchedResultsController work with NSDictionaryResultType?
我使用 Core Data 将商店存储为 Entity
商店,并且在我有一个 Attribute
城市的每个商店中,我想按城市对商店进行分组并显示一个 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"
}
...
我使用 Core Data 将商店存储为 Entity
商店,并且在我有一个 Attribute
城市的每个商店中,我想按城市对商店进行分组并显示一个 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"
}
...