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

2つの方法の違いについては、 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;
}];

Appleの例)

2番目の方法は次のようになります。

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

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