Iphone 计算不同的结果

Iphone 计算不同的结果,iphone,core-data,count,distinct,Iphone,Core Data,Count,Distinct,我有一个请求,它允许我在4个不同字段的组合中检索不同的值,并且工作正常 NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; [requ

我有一个请求,它允许我在4个不同字段的组合中检索不同的值,并且工作正常

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
[request setEntity:entity]; 
[request setReturnsDistinctResults:YES];
[request setResultType:NSDictionaryResultType];
NSDictionary *entityProperties = [entity propertiesByName];

NSMutableArray *properties = [NSMutableArray arrayWithObject:[entityProperties objectForKey:@"A"]];
[properties addObject:[entityProperties objectForKey:@"B"]];
[properties addObject:[entityProperties objectForKey:@"C"]];
[properties addObject:[entityProperties objectForKey:@"D"]];
//[properties addObject:countExpressionDescription];
[request setPropertiesToFetch: properties];
现在,我想添加一个字段,用于统计已分组的元素数(在distinct之前)。我曾尝试通过以下方式实现这一点,但它不起作用:

NSExpression *kpe1 = [NSExpression expressionForKeyPath:@"A"];
NSExpression *kpe2 = [NSExpression expressionForKeyPath:@"B"];
NSExpression *kpe3 = [NSExpression expressionForKeyPath:@"C"];
NSExpression *kpe4 = [NSExpression expressionForKeyPath:@"D"];

NSMutableArray *keyPathExpressions = [NSMutableArray arrayWithObject:kpe1];
[keyPathExpressions addObject:kpe2];
[keyPathExpressions addObject:kpe3];
[keyPathExpressions addObject:kpe4];
NSExpression *countForFields = [NSExpression expressionForFunction:@"count:" arguments:keyPathExpressions];

NSExpressionDescription *countExpressionDescription = [[NSExpressionDescription alloc] init];
[countExpressionDescription setName:@"countID"];
[countExpressionDescription setExpression:countForFields];
[countExpressionDescription setExpressionResultType:NSInteger64AttributeType];
我甚至不确定它是否可行,你有什么想法吗

非常感谢