【发布时间】:2014-02-15 03:57:38
【问题描述】:
如何按我正在获取的实体类别中的方法返回的值对获取的结果进行排序?
在我的类别中,我将实体的一对多关系中的几个值相加,然后除以关系中的对象数,有效地创建了一个平均值,我在我的类别方法中作为浮点值返回。
这是我的代码:
在 Category.h 中
- (float)smallPenaltyAvg;
在 Category.m 中
- (float)smallPenaltyAvg{
float smallPenaltyAvg = 0;
for (Match *mtch in self.matches) {
smallPenaltyAvg += [mtch.penaltySmall floatValue];
}
if ([self.matches count] > 0) {
smallPenaltyAvg = (float)smallPenaltyAvg/(float)[self.matches count];
}
return smallPenaltyAvg;
}
当我在我创建的 Core Data Table View Controller 类中调用它时......
NSFetchRequest *poolRequest = [[NSFetchRequest alloc] initWithEntityName:@"Team"];
poolRequest.predicate = [NSPredicate predicateWithFormat:@"regionalIn.name = %@", _regionalToDisplay];
poolRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"smallPenaltyAvg" ascending:YES]];
我在Category.h 文件本身之外的每个文件中导入了Category.h 文件。
它给了我以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath smallPenaltyAvg not found in entity <NSSQLEntity Team id=5>
我不能这样做吗?
如果我是,我做错了什么?
【问题讨论】:
标签: core-data nsfetchrequest nssortdescriptor nsfetchedresultscontroller