简体   繁体   English

NSSortDescriptor-顺序更改了吗?

[英]NSSortDescriptor - has the order changed?

I have an NSFetchRequest that is getting objects from core-data and sorting them using a trivial NSSortDescriptor . 我有一个NSFetchRequest ,它从核心数据中获取对象并使用琐碎的NSSortDescriptor对它们进行排序。 I use a couple of different ones depending on what the user selects, but they are like this: 根据用户的选择,我使用了几种不同的方法,但是它们是这样的:

    NSMutableArray *sortDescriptors = [NSMutableArray array];
    NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"ownerName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"petName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    [sortDescriptors addObject:sortDescriptor1];
    [sortDescriptors addObject:sortDescriptor2];
    [fetchRequest setSortDescriptors:sortDescriptors];

Everything works as expected on iOS5 and 5.1, however when I test exactly the same code on an iPad running iOS4.2.1 (or the 4.3 simulator) the order of all the first sort descriptor is reversed. 一切都能在iOS5和5.1上正常运行,但是,当我在运行iOS4.2.1(或4.3模拟器)的iPad上测试完全相同的代码时,所有第一个排序描述符的顺序都将颠倒。 In this example, the owners names are ordered Z to A, but the pets are correctly ordered A to Z! 在此示例中,所有者名称按从Z到A的顺序排列,但是正确地将宠物从A到Z的顺序排列!

Was there a bug in earlier iOS versions that caused the ascending boolean to be the wrong way around? 有没有在导致早期的iOS版本的bug, ascending布尔被周围走错了路? Alternatively, is there something else I may be doing incorrectly? 另外,还有其他我可能做错的事情吗?

EDIT: The first key (ownerName) is used as the NSFetchedResultsController section. 编辑:第一个键(ownerName)用作NSFetchedResultsController部分。 Not sure if this is relevant. 不知道这是否相关。

Thanks Craig 谢谢克雷格

Just guessing. 只是猜测。

I have had good results without specifying caseInsensitiveCompare: . 没有指定caseInsensitiveCompare:我得到了很好的结果。

Also, strictly speaking NSFetchRequest expects an NSArray as the sortDescriptors argument, not NSMutableArray . 同样,严格来说, NSFetchRequest希望将NSArray作为sortDescriptors参数,而不是NSMutableArray In all of Apple's sample code it is normally something like 在Apple的所有示例代码中,通常都类似于

[fetchRequest setSortDescriptors:
   [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];

I have had errors like that (using mutable instead of immutable), but often it also just works. 我曾经遇到过这样的错误(使用可变的而不是不可变的),但是通常它也可以正常工作。 Maybe changing that is worth a try. 也许改变是值得尝试的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM