目前,我有一个在表视图中显示的核心数据模型。如何按category
对结果进行排序,{{1}}是核心数据模型中的一个条目?提前谢谢!
答案 0 :(得分:2)
您是否阅读了Core Data Programming Guide?
秘诀是在您想要排序的财产上为NSSortDescriptor设置NSFetchRequest。
例如:
编辑:(文档中的整个示例)
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Employee" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
// Set example predicate and sort orderings...
NSNumber *minimumSalary = ...;
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"(lastName LIKE[c] 'Worsley') AND (salary > %@)", minimumSalary];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"firstName" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];
NSError *error;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
// Deal with error...
}