OGeek|极客世界-中国程序员成长平台

标题: ios - "localizedStandardCompare"取决于 iOS 设备的语言偏好 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:26
标题: ios - "localizedStandardCompare"取决于 iOS 设备的语言偏好

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey"name" ascending:YES selectorselector(localizedStandardCompare];

[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor,nil]];

我正在开发一个包含名称列表的应用程序,其中所有名称都使用 Íslenska(冰岛)语言。 如果在设置中为应用程序选择的语言是 Íslenska(Icelandic),则上面的代码可以正常工作,但如果语言不是 Íslenska(Icelandic),则排序描述符会失败。

有没有办法解决这个依赖问题?



Best Answer-推荐答案


这应该创建一个 sortDescriptor,它将使用 is_IS 语言环境比较字符串。

NSSortDescriptor* sortDescriptior =[[NSSortDescriptor alloc] initWithKey"name" ascending:YES comparator:^(NSString* str1, NSString* str2) {   

static NSStringCompareOptions comparisonOptions =
    NSCaseInsensitiveSearch | NSNumericSearch |
    NSWidthInsensitiveSearch | NSForcedOrderingSearch;

    return [str1 compare:str2 options:comparisonOptions range:NSMakeRange(0, str1.length) locale:[NSLocale localeWithLocaleIdentifier"is_IS"]];
}];

关于ios - "localizedStandardCompare"取决于 iOS 设备的语言偏好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32859254/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4