“localizedStandardCompare”取决于iOS设备的语言首选项

时间:2015-09-30 06:29:02

标签: ios

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

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

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

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

1 个答案:

答案 0 :(得分:2)

这应创建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"]];
}];
相关问题