【发布时间】:2014-05-15 08:44:27
【问题描述】:
我想做以下事情:
Select distinct department
From employees
Order By department
(按部分排序是可选的)。
Employees 包含 Person 对象,这些对象具有名称和部门属性。
我已经查看了很多这样的问题,但是它对我不起作用。这就是我正在尝试的方式:
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
上面的代码给了我一个包含 3 个 NSDictionaries 的数组,像这样:
{
department = D1
}
{
department = D1
}
{
department = D2
}
【问题讨论】:
-
你期望的结果是什么?
-
好吧,执行我用SQL写的select,它只会返回[D1,D2]。
标签: objective-c core-data nsfetchrequest