在核心数据中排序,如何?

时间:2013-11-10 01:04:41

标签: objective-c sorting core-data

目前,我有一个在表视图中显示的核心数据模型。如何按category对结果进行排序,{{1}}是核心数据模型中的一个条目?提前谢谢!

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...
}