首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >setPropertiesToFetch返回“无效键盘表达式”错误

setPropertiesToFetch返回“无效键盘表达式”错误
EN

Stack Overflow用户
提问于 2016-02-08 06:41:29
回答 1查看 937关注 0票数 2

我试图以其最基本的形式使用组核心数据功能:

代码语言:javascript
运行
复制
- (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];
...

在运行此代码时,我会得到以下错误:

由于未命名的异常“NSInvalidArgumentException”终止应用程序,原因:“无效的键盘表达式( )、名称maxReview、isOptional 1、isTransient 0、实体(null)、renamingIdentifier maxDepartment、验证谓词( )、警告()、versionHashModifier (null) userInfo { })传递给setPropertiesToFetch

我想我错过了一些API,所以任何帮助都会非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-08 14:30:01

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

代码语言:javascript
运行
复制
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];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35263730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档