2014-05-15 14 views
1

의 고유 값을 가져옵니다.코어 데이터는 다음 작업을 수행 할 싶습니다 재산

직원에는 이름과 부서 속성이있는 개인 개체가 들어 있습니다.

나는 이런 질문을 많이 보았지만 저에게는 효과가 없습니다. 의 내용에 대해

AppDelegate *applicationDelegate = [[NSApplication sharedApplication] delegate]; 
NSManagedObjectContext *context = [applicationDelegate managedObjectContext]; 

NSEntityDescription *personEntity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context]; 

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Person"]; 
fetchRequest.resultType = NSDictionaryResultType; 
fetchRequest.returnsDistinctResults = YES; 
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[personEntity propertiesByName] objectForKey:@"department"]]; 

NSError *error = nil; 
NSArray *result = [context executeFetchRequest:fetchRequest error:&error]; 
if (error) { 
    ALog(@"%@",error); 
} else { 
    return result; 
} 

: 이것은 내가 할 노력하고있어 어떻게

Name | Department 
P1 | D1 
P2 | D1 
P3 | D2 

위의 코드는 저에게이 추천 NSDictionaries의 배열, 제공 :

{ 
    department = D1 
} 
{ 
    department = D1 
} 
{ 
    department = D2 
} 
+1

예상되는 결과는 무엇입니까? – KudoCC

+0

글쎄, SQL에서 쓰는 select를 실행하면 [D1, D2] 만 반환됩니다. –

답변

0

동일한 문제가있는 사람에게 :

내가 발견 한 것은 영구 저장소 유형이 NSSQLiteStoreType 인 경우 returnDistinctResult 님의 작품. 그러나 NSXMLStoreType의 고유 값은 작동하지 않습니다.

Credit to this answer.

관련 문제