本地化的string比较
NSString
的localizedCaseInsensitiveCompare:
和localizedStandardCompare:
方法有什么区别?
我读了参考书,但没有得到一个适当的想法,哪一个使用。
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]];
所以,主要区别在于如何比较string中的数字。
使用localizedCaseInsensitiveCompare:
比较以下3个string将导致以下顺序:
"Foo2.txt", "Foo25.txt", "Foo7.txt"
另一方面,使用localizedStandardCompare:
比较它们会导致以下顺序:
"Foo2.txt", "Foo7.txt", "Foo25.txt"
当localizedCaseInsensitiveCompare:
方法一直存在时, localizedStandardCompare:
最近才被添加(OS X 10.6)。 Finder使用数字方法对文件名进行sorting,在添加localizedStandardCompare:
之前,开发人员没有一个简单的方法来确保他们可以像Finder那样对string进行sorting。
当确定使用哪一个时,如果你比较的string代表文件名,那么你应该倾向于使用localizedStandardCompare:
后者类似于OS X上的Finder。
localizedCaseInsensitiveCompare:
与localizedCompare:
类似localizedCompare:
它们都返回一个NSComparisonResult
值,该值指示接收者的词法sorting和使用本地化比较的另一个给定string。
不同之处在于前者case insensitive
。 即比较string时忽略大写和小写字母的不同用法。
localizedStandardCompare
这比较string由Finder应用程序sorting。