在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];
}];