3

执行此代码时:

NSSortDescriptor *sortDescriptor = [Characteristic sortDescriptor];
[workingSet sortUsingComparator:[sortDescriptor comparator]];

我收到此错误:

*** -[NSMutableOrderedSet sortUsingComparator:]: comparator cannot be nil

sortDescriptor不是零,所以我不知道为什么这不起作用。

我可以用下面的代码解决这个问题,它完美地工作:

NSSortDescriptor *sortDescriptor = [Characteristic sortDescriptor];
NSArray *workingArray = [[workingSet array] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
workingSet = [NSMutableOrderedSet orderedSetWithArray:workingArray];
4

1 回答 1

9

查看NSArray参考以了解这两种方法之间的区别

第一种方法的典型用法是这样的

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

(来自苹果的例子)

第二种方法更像是:

NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:nameSort];

NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
于 2012-02-10T00:41:46.650 回答