这是我要为Core Data
编写的查询的SQL版本:
SELECT ROUNDNAME
FROM MODELSCHEDULEFIXTURE
GROUP BY ROUNDNAME
ORDER BY MATCHID
我使用了以下代码,但该应用程序崩溃了。
-(NSArray *)getRound
{
NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
NSFetchRequest *fetch = [NSFetchRequest new];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"modelschedulefixture" inManagedObjectContext:context];
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"roundName"];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[fetch setResultType:NSDictionaryResultType];
NSError *error = nil;
NSArray *array= [context executeFetchRequest:fetch error:&error];
return array;
}
最佳答案
尝试一下。
-(NSArray *)getRound{
NSArray *array=nil;
NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"modelschedulefixture"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"modelschedulefixture" inManagedObjectContext:context];
NSSortDescriptor *sortDescreptorRound =[[NSSortDescriptor alloc]initWithKey:@"matchid" ascending:YES];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescreptorRound]];
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"roundname"];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[fetch setResultType:NSDictionaryResultType];
NSError *error = nil;
array = [context executeFetchRequest:fetch error:&error];
return array;
}
关于ios - 按名称分组的核心数据获取属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22498623/