1

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

4

1 回答 1

3

你读过核心数据编程指南吗?

秘诀是通过您希望排序的属性在您的NSFetchRequest上设置NSSortDescriptor 。 例如:

编辑:(文档中的整个示例)

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...
}
于 2013-11-10T06:22:34.210 回答