localizedCaseInsensitiveCompare:
相当于:
[aString compare:otherString
options:NSCaseInsensitiveSearch
range:NSMakeRange(0,aString.length)
locale:[NSLocale currentLocale]];
localizedStandardCompare:
基本上相当于:
[aString compare:otherString
options:NSCaseInsensitiveSearch | NSNumericSearch
range:NSMakeRange(0,aString.length)
locale:[NSLocale currentLocale]];
因此,主要区别在于如何比较字符串中的数字。
使用比较以下 3 个字符串localizedCaseInsensitiveCompare:
将导致以下顺序:
"Foo2.txt",
"Foo25.txt",
"Foo7.txt"
另一方面,使用比较它们localizedStandardCompare:
将导致以下顺序:
"Foo2.txt",
"Foo7.txt",
"Foo25.txt"
虽然localizedCaseInsensitiveCompare:
方法一直存在,localizedStandardCompare:
最近才添加(OS X 10.6)。 Finder 使用数字方法对文件名进行排序,并在添加之前localizedStandardCompare:
,开发人员没有一种简单的方法来确保他们可以像 Finder 那样对字符串进行排序。
在确定使用哪一个时,如果您要比较的字符串代表文件名,那么您绝对应该倾向于使用localizedStandardCompare:
.