2015-01-27 1 views
0

코어 데이터를 사용하여 상점을 저장합니다. Entity 상점과 모든 상점에 Attribute 도시가 있으며, 도시별로 상점을 그룹화하고 모든 도시에 UITableView을 선물하고 싶습니다. 나는 NSFetchedResultsController을 사용하여 데이터를 가져오고 UITableView을 새로 고칩니다. 도시를 그룹화하고자하므로 resultTypeNSDictionaryResultType으로 설정했습니다. 그러나 지금 objectAtIndexPath을 내 NSFetchedResultsController에 사용하면 NSKnownkeysdictionary1 내 질문은 NSFetchedResultsControllerNSDictionaryResultType으로 처리 할 수 ​​있습니까? 아니면이 경우 NSFetchedResultsController을 사용해서 다른 방법을 사용해야합니까?NSFetchedResultsController를 NSDictionaryResultType과 함께 사용할 수 있습니까?

답변

1

적절한 값을 가져 오려면 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" 
    } 
... 
+0

,이 같은 사전 유형에 작동하는 것입니다,하지만 어떻게'cellForRowAtIndexPath'에 대한 사전 접근 할 것 같아? 그것은'NSFetchedResultsController'의'objectAtIndexPath'와'fetchedObjects'가 실제로 그것을 할 수없는 것처럼 보입니다 – Georgi

+0

가져온 객체는 사전의 배열을 제공해야합니다. – Sandeep

+0

예! 모든 것이 완벽하게 작동합니다. – Georgi