2

我收到一个错误(iOS):由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“使用 GROUP BY 组件的查询中的 SELECT 子句只能包含在 GROUP BY 或聚合函数中命名的属性(logtijd 不在 GROUP BY 中) )'

在下面的代码中,我错过了什么吗?我使用 max: 聚合函数,所以我会得到这个错误吗?“logtijd” - 核心数据日期 (NSDate)。

max: 功能是否适用于 iOS?请参阅此链接: https ://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSExpression_Class/Reference/NSExpression.html#//apple_ref/occ/clm/NSExpression/expressionForFunction:arguments :

聚合函数 @"average" 有效!

   NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:[NSEntityDescription entityForName:@"Tijden" inManagedObjectContext:self.context]];

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"logtijd"];    
NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];

NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

[expressionDescription setName:@"maxlogtijd"];
[expressionDescription setExpression:maxExpression];
[expressionDescription setExpressionResultType:NSDateAttributeType];

    [request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:[NSArray arrayWithObjects:@"starttijd", expressionDescription, nil]];
    [request setPropertiesToGroupBy:[NSArray arrayWithObject:@"starttijd"]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"atleet == %@ AND workout == %@", self.atleetGeselecteerd, self.workoutGeselecteerd]];

    return [self.context executeFetchRequest:request error:nil];
4

0 回答 0