Objective-c 挪威字母 æ ø å,NSArray SortedArrayUsingSelector:问题

发布于 2024-12-10 18:48:13 字数 553 浏览 0 评论 0原文

我已读过 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html%23//apple_ref/doc/uid/20000132-SW5

但我没有找到解决我的问题的方法。

我写道:

NSArray *sorted = [unsorted sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

显示排序顺序 AZ,A 中为 Æ 和 Å,O 中为 Ø。 它应该是 AZ-Æ-Ø-Å,因为我使用了本地化

I have read
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html%23//apple_ref/doc/uid/20000132-SW5

But I did not find a solution to my problem.

I wrote:

NSArray *sorted = [unsorted sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

shows the sorted order A-Z with Æ and Å among A, Ø among O.
It is supposed to be A-Z-Æ-Ø-Å, since I used localized.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋葬我深情 2024-12-17 18:48:13

覆盖整个应用程序的用户语言通常不被认为是强制特定操作的区域设置的好方法,因为用户的首选项应确定数据的排序方式。例如,如果您有一个语言类或一个有意使用双语言的程序,您可能会发现仅针对程序的某些元素覆盖排序顺序对用户很有用。尽管这需要更多工作,但如果您需要确保特定字符串始终按特定区域设置的顺序排序,则应使用比较的长形式:

- (NSComparisonResult)compare:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)range locale:(id)locale

调用获取挪威语的区域设置:

NSLocale *noLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"no_NO"] autorelease];

您可以通过使用以下 ,您最终会调用:

NSArray *sorted = [unsorted sortedArrayUsingComparator :^(id obj1, id obj2) {
    NSRange theRange = NSMakeRange( 0, [obj1 length]);
    return [obj1 compare: obj2 options: NSCaseInsensitiveSearch range: theRange locale: noLocale];
}];

这不会为程序的其余部分设置默认语言,但它将在该特定比较中显式使用指定的区域设置。

Overriding the user's language for the entire application is generally not considered to be a good way to force the locale for a particular operation, as the user's preferences should determine how data is sorted. If, for example, you have a language class or an otherwise intentionally-dual-language program), you might find it useful to the user to override the sorting order just for certain elements of the program. Although it's a bit more work, if you need to make sure that the particular strings are always sorted in order for a particular locale, you should use the long form of the compare:

- (NSComparisonResult)compare:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)range locale:(id)locale

You can get the locale for Norwegian by using the call:

NSLocale *noLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"no_NO"] autorelease];

So, you'd end up calling:

NSArray *sorted = [unsorted sortedArrayUsingComparator :^(id obj1, id obj2) {
    NSRange theRange = NSMakeRange( 0, [obj1 length]);
    return [obj1 compare: obj2 options: NSCaseInsensitiveSearch range: theRange locale: noLocale];
}];

This will not set the default language for the rest of the program, but it will explicitly use the specified locale in that particular comparison.

蓝眸 2024-12-17 18:48:13

我通过在 -viewDidLoad 中插入这一行解决了这个问题:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"no"] forKey:@"AppleLanguages"];

I solved it by inserting this line in -viewDidLoad :

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"no"] forKey:@"AppleLanguages"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文