如何在核心数据中逐组应用

我正在实现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: 

除了以下expression式之外,还必须将propertiesToFetch中的其他属性添加到group by子句中:

 [fetch setPropertiesToGroupBy:[NSArray arrayWithObjects:clsName, clsDate, startTime, endTime, nil]];