2012-10-31 176 views
0

我目前正在使用核心數據的應用程序。我處理的問題是取得與謂詞數據,如下圖所示例子:核心數據 - 謂詞獲取類別

[[self managedObjectContext] fetchObjectsForEntityName:@"Employee" withPredicate: 
@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary]; 

我想要做的是從某一個數據集這樣獲取不同類別的數量:

實體的人:

-Name(字符串)

-Location(字符串)

- 年齡(INT)

因此,舉例來說,如果我有數據集中的3個樣品: [麗莎,瑞典,30] [麥克,美國,24] [雅各,英格蘭,28]

我現在想獲取不同位置的數, 這可能嗎?

非常感謝

羅伯特

回答

1

在FetchResults控制器的方法,添加以下代碼。

[fetchRequest setReturnsDistinctResults:YES]; 

[fetchRequest setResultType:NSDictionaryResultType]; 

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"value"]]]; 

從returnsDistinctResults的文檔:

This value is only used if a value has been set for propertiesToFetch. 

從propertiesToFetch的文檔:

This value is only used if resultType is set to NSDictionaryResultType. 

從與resultType的文檔:

The default value is NSManagedObjectResultType.