不支持的NSSortDescriptor(不支持比较器块)

fetchedResultsController设置NSSortDescriptor iam时得到此错误不支持NSSortDescriptor(不支持比较器块)

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alarm" inManagedObjectContext: managedObjectContext]; [fetchRequest setEntity:entity]; //Below code is not working and causing error. sorting use the hours&seconds part of the time attribute NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components1 = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:obj1]; NSDateComponents *components2 = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:obj2]; NSDate *date1 = [calendar dateFromComponents:components1]; NSDate *date2 = [calendar dateFromComponents:components2]; return [date1 compare:date2]; }]; 

您不能在任何地方使用带有比较器块的排序描述符 – 例如,不能使用Core Data提取。

但是,当您过滤普通数组时,它们可以正常工作。

除此之外 – 那里有一个我忽略的问题吗?

正如Monolo所说,你不能将比较器块与Core Data一起使用。 但是,您可以使用:

 [NSSortDescriptor sortDescriptorWithKey:(NSString *) ascending:(BOOL) selector:(SEL)] 

如果您不使用标准选择器,则需要扩展模型。

例如,我只需要订购字符串“a la Finder”(即1,2,9,10,而不是1,10,2,9)并且我使用了

 request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:myEntity ascending:NO selector:@selector(localizedStandardCompare:)]]; 

看一下Apple的NSSortDescriptor Class Reference

快乐编码:)