gpt4 book ai didi

ios - setPropertiesToFetch 返回 'Invalid keypath expression' 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:44 25 4
gpt4 key购买 nike

我正在尝试以最基本的形式使用核心数据分组功能:

- (NSFetchedResultsController *)fetchedResultsController {

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}

NSExpression *reviewNumKeyExpression = [NSExpression expressionForKeyPath:@"review_num"];
NSExpression *reviewMaxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:reviewNumKeyExpression]];

NSExpressionDescription *maxReviewED = [[NSExpressionDescription alloc] init];
[maxReviewED setName:@"maxReview"];
[maxReviewED setExpression:reviewMaxExpression];
[maxReviewED setExpressionResultType:NSInteger32AttributeType];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Group" inManagedObjectContext:managedObjectContext];

[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setEntity:entity];

NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setPropertiesToGroupBy:propertiesToFetch];
...

运行此代码时,出现以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath expression ((), name maxReview, isOptional 1, isTransient 0, entity (null), renamingIdentifier maxDepartment, validation predicates ( ), warnings ( ), versionHashModifier (null) userInfo { }) passed to setPropertiesToFetch

我想我在 API 中遗漏了一些东西,所以非常感谢任何帮助!

最佳答案

我认为错误消息是错误的:问题出在 setPropertiesToGroupBy,而不是 setPropertiesToFetch。您不能按计算值 (maxReview) 分组,但我怀疑您是否真的想这样做:如果您想要每个 groupIdreview_num 的最大值>,你只需要propertiesToGroupBy中的groupId:

NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
NSArray *propertiesToGroupBy = [NSArray arrayWithObjects:groupId, nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setPropertiesToGroupBy:propertiesToGroupBy];

关于ios - setPropertiesToFetch 返回 'Invalid keypath expression' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35263730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com