ios - NSDictionary allKeys,然后使用选择器与 keysSortedByValueUsingSelector 进行排序

标签 ios objective-c ios6 nsdictionary

我试图将我的 NSDictionary 中所有键的数组放入一个数组中,并使用 localizedCaseInsensitiveCompare 进行排序。我首先尝试做:

NSArray *test = [myDict keysSortedByValueUsingSelector@selector(localizedCaseInsensitiveCompare:)]]; 

我不断收到 NSCFNumber localizedCaseInsensitiveCompare:]: unrecognized selector。我仔细检查了一下,我所有的 2 个键(目前)都是字符串。

我不得不改用 doing 来让它工作:

NSArray *items = @[[[languages allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];

这是为什么?!

谢谢!

最佳答案

这两种情况有不同的排序。

NSArray *test = [myDict keysSortedByValueUsingSelector@selector(localizedCaseInsensitiveCompare:)]];

这种变体排序(来自文档):

Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values.

您尝试获取一个数组,首先使用选择器对值进行排序,然后获取键列表。我猜,你字典的对象不是 NSString 并且它们对这个选择器一无所知。

NSArray *items = @[[[languages allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];

在这里你做对了。您获得键数组(它们是 NSString),然后使用右选择器对其进行排序。

关于ios - NSDictionary allKeys,然后使用选择器与 keysSortedByValueUsingSelector 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17569537/

相关文章:

ios - subscribeNext 和 bindNext 的区别

objective-c - 如何在 ReactiveCocoa 5 中将 RACSignal 转换为 SignalProducer?

objective-c - 如何以编程方式检测自动引用计数?

iphone - Segue 不适用于 iOS6

ios - 如何在矢量化图像周围制作动画渐变边框(适用于 iPad 的 Objective-C)?

c# - Xamarin.Forms 中带有图像的短信

ios - 如何从一致的类中正确设置协议(protocol)中的属性?

iphone - 在 UITableView 中展开部分,当展开多个部分时会崩溃

ios - 在 IOS 的 ImageView 中从 API 多次加载图像

ios6 - initWithAppID 不使用原生 Facebook 登录与 iOS SDK 3.1.1