3
当执行此代码:排序NSOrderedSet使用NSSortDescriptor
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];
那么,为什么不NSSortDescriptor的'-comparator'方法生产出符合'-sortedArrayUsingComparator工作的比较:'或'-sortUsingComparator:'? – paulmelnikow 2012-02-11 19:22:20
尽管你的sortDescriptor是'!= nil',我猜测'-comparator'返回'nil'。从文档中不太清楚,但它看起来像只返回一个比较器,如果您创建了一个例如一个比较器的描述符。使用'sortDescriptorWithKey:升序:比较器:'。 – 2012-02-12 11:42:31
呃。这就说得通了。但是,呃。 – paulmelnikow 2012-02-14 04:38:56