计算不同的结果

时间:2010-12-06 09:41:54

标签: iphone core-data count distinct

我有这个请求,允许我通过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];

现在我想添加一个字段,该字段计算已分组的元素数(在不同之前)。我尝试用以下方法完成这项工作,但它不起作用:

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];

我甚至不确定它是否可行,你有什么想法吗?

非常感谢

0 个答案:

没有答案