Objective-c挪威字母表æøå,NSArray sortedArrayUsingSelector:Issue

时间:2011-10-18 21:39:21

标签: objective-c sorting nsarray

我读过 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:)];

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

2 个答案:

答案 0 :(得分:4)

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

- (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];
}];

这不会为程序的其余部分设置默认语言,但会在该特定比较中明确使用指定的语言环境。

答案 1 :(得分:0)

我通过在-viewDidLoad中插入此行来解决它:

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