localizedCaseInsensitiveCompare:和caseInsensitiveCompare之间的区别是什么?

时间:2012-01-08 10:26:37

标签: iphone objective-c ios cocoa-touch ipad

我打算使用以下代码行:

[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(caseInsensitiveCompare:)];

'caseInsensitiveCompare'是我习惯在字符串上使用的方法。但是,示例显示我正在使用:

[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

(差异是本地化的词)。这个词做了什么 - “本地化”方法与常规方法有何不同?

Apple开发人员文档对这两种方法的不同之处并不十分了解。

4 个答案:

答案 0 :(得分:14)

这意味着比较器在比较时使用国家字符集 例如,波兰语有字母Ł,在国家字符集中,它在L和M之间。

例如,当我们有字符串时:Ltest,Łtest,Mtest,Ztest字符串:

caseInsensitiveCompare给出结果:Ltest,Mtest,Ztest,Łtest
localizedCaseInsensitiveCompare给出结果:Ltest,Łtest,Mtest,Ztest

答案 1 :(得分:10)

NSString提供了两种方法,caseInsensitiveCompare和localizedCaseInsensitiveCompare。

某些区域设置可能会定义不同的排序条件。如果您正在处理针对各种语言环境本地化的文本,请使用本地化版本。否则,请使用标准版本。

答案 2 :(得分:5)

localizedCaseInsensitiveCompare:caseInsensitiveCompare:的本地化版本。因为它在英语语言环境中没有太大区别,所以例如波兰语为ł, ą, ę等,西班牙语为ñ, é, á等,法语为ç, è, ê等。根据语言环境,它们在字母表中的位置不同。

答案 3 :(得分:5)

localizedCaseInsensitiveCompare:方法将在对数据进行排序时使用当前区域设置([NSLocale currentLocale])的任何规则。这些规则通常包括编号优先级,非ASCII字符排序等。

基本上,除非您期望基于字符代码进行排序,否则您应该使用本地化方法。

如果您之前没有使用本地化,我建议您查看以下链接: