0
我希望NSFetchRequest
为我的UITableViewController
根据特定的属性对相似的记录(实体)进行分组。我目前分两步进行,但我相信有可能会使用+ (NSExpression *)expressionForAggregate:(NSArray *)collection
。使用NSExpression组合实体并返回一个NSArray或NSArray元素
有人可以帮助使用适当的代码吗?
下面是返回一个数组的数组,我的两个步骤的代码:
+(NSArray *)getTopQforDogByProgram2:(Dog *)dog
inProgram:(RunProgramTypes)programType
inManagedContext:(NSManagedObjectContext *)context {
NSString *searchString;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Run"];
request.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"dog.callName = '%@'",dog.callName]];
NSSortDescriptor *classSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"runClass" ascending:NO];
request.sortDescriptors = [NSArray arrayWithObject:classSortDescriptor];
NSError *error = nil;
NSArray *dataArray = [context executeFetchRequest:request error:&error];
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
if ([dataArray count] > 0) {
NSMutableArray *pointArray = [[NSMutableArray alloc] init];
for (Run *run in dataArray) {
if (! [returnArray count]) {
[pointArray addObject:run];
[returnArray addObject:pointArray];
} else {
BOOL wasSame = FALSE;
for (NSMutableArray *cmpArray in returnArray) {
Run *cmpRun = [cmpArray lastObject];
if (cmpRun.runClass == run.runClass) {
[cmpArray addObject:run];
wasSame = TRUE;
break;
}
}
if (! wasSame) {
pointArray = [[NSMutableArray alloc] init];
[pointArray addObject:run];
[returnArray addObject:pointArray];
}
}
}
}
return returnArray;
}