我试图将我的 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/