我正在实现tableview,我想在tableview的部分显示类名,我正在尝试获取 使用核心数据实现的数据库中的类值,我想使用classname上的group by子句获取数据这是我的代码
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//create managed object context
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Boking_Detail"];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Boking_Detail"
inManagedObjectContext:context];
NSAttributeDescription* clsName = [entity.attributesByName objectForKey:@"classname"];
NSAttributeDescription* clsDate = [entity.attributesByName objectForKey:@"classdate"];
NSAttributeDescription* startTime = [entity.attributesByName objectForKey:@"starttime"];
NSAttributeDescription* endTime = [entity.attributesByName objectForKey:@"endtime"];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: @"count"];
// [expressionDescription setExpression: countExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:clsName,clsDate,startTime,endTime, expressionDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:clsName]];
[fetch setResultType:NSDictionaryResultType];
NSError* error = nil;
NSArray *results = [context executeFetchRequest:fetch error:&error];
NSLog(@"result array count %i",results.count);
NSLog(@"result array values %@",results);
我收到此错误
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions ((<NSAttributeDescription: 0xc288680>), name classdate, isOptional 1, isTransient 0, entity Boking_Detail, renamingIdentifier classdate, validation predicates (
), warnings (
), versionHashModifier (null)
userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null) is not in the GROUP BY)'
*** First throw call stack:
答案 0 :(得分:8)
您还必须将propertiesToFetch
中的其他属性添加到group by子句中,但表达式除外:
[fetch setPropertiesToGroupBy:[NSArray arrayWithObjects:clsName, clsDate, startTime, endTime, nil]];