NSLocale setPreferredLanguage
I seem to be going in circles around this problem and I am heading no where now.
I am trying to force the language of my app (to swedish) as shown below.
@autoreleasepool {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"sv", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
I have done it in the main method, so that preference is set as soon as the application is launched.
Then when i want to sort an array alphabetically i use NSSortDescriptor
with selector localizedCaseInsensitiveCompare:
.
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"iName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor];
self.ingredientsList = [NSMutableArray arrayWithArray:[self.ingredientsList sortedArrayUsingDescriptors:descriptors]];
But my problem is sorting with my local language does not happen the first time. But if I restart the app, the sorting is according to the language (Swedish). Why doesnt it happen the first time?
What am i doing wrong?
Help/suggestions will be appreciated. Thanks
@DarkDust is correct. You should not try to modify the locale itself. That's not under your control, and as you're seeing, it doesn't work very well. Most importantly, if you just want to modify one sort operation, this is a very big hammer to do that.... You could potentially play with the various localization Info.plist
entries (particularly CFBundleLocalizations
) to get this to work right, but not just to sort a single list.
What you want to use is compare:options:range:locale:
, which allows you to pass a specific locale. You can use this method inside of a sortedArrayUsingSelector:
or sortedArrayUsingComparator:
.
Alternately, you could use a category to add a custom swedishCaseInsensitiveCompare:
to NSString
. You could then use NSSortDescriptor
as you are currently.
Example. ä
is sorted after a
in English, but after z
in Swedish:
@interface NSString (SwedishSorting)
- (NSComparisonResult)caseInsensitiveSwedishCompare:(id)other;
@end
@implementation NSString (SwedishSorting)
- (NSComparisonResult)caseInsensitiveSwedishCompare:(id)other;
{
return [self compare:other options:NSCaseInsensitiveSearch range:NSMakeRange(0, self.length) locale:[[NSLocale alloc] initWithLocaleIdentifier:@"sv"]];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *array = [@"Jag är en test av sortering" componentsSeparatedByString:@" "];
NSArray *englishSort = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
NSLog(@"English = %@", englishSort);
NSArray *swedishSort = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(caseInsensitiveSwedishCompare:)]]];
NSLog(@"Swedish = %@", swedishSort);
}
return 0;
}
链接地址: http://www.djcxy.com/p/36110.html
上一篇: NSSortDescriptor问题