Custom Selector For NSSortDescriptor and NSFetchedResultsController

后端 未结 2 800
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 17:10

I am running into a bit of trouble and I was wondering if someone could provide some guidance.

I am attempting to create section headers with the following order: [\

2条回答
  •  日久生厌
    2021-01-05 17:34

    The NSSortDescriptor constructor that you're using attempts to call the specified selector on the objects held at the property named @"states". I'm not sure what this value is, but it looks like they might be NSSortDescriptor objects based on the error you're seeing.

    Instead of trying to find a selector on an object which may or may not exist, use the block constructor:

    You should also really consider rewriting your comparison with integers or something to make it smaller:

    static NSArray *stateOrder = @[@"Push", @"Busy", @"Finished", @"Cancelled"];
    
    NSSortDescriptor *sortStates = [NSSortDescriptor sortDescriptorWithKey:@"states"
                                                        ascending:NO
                                                       comparator:^(id obj1, id obj2) {
      NSInteger state1 = [stateOrder indexOfObject:obj1];
      NSInteger state2 = [stateOrder indexOfObject:obj2];
      if (state1 < state2) {
        return (NSComparisonResult)NSOrderedAscending;
      } else if (state1 > state2) {
        return (NSComparisonResult)NSOrderedDescending;
      }
      return (NSComparisonResult)NSOrderedSame;
    }];
    

提交回复
热议问题