defs = [NSUserDefaults standardUserDefaults];
languages = [[NSMutableArray alloc]init];
languages = [defs objec开发者_C百科tForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:14];
NSString *chn = @"zh-Hans";
NSString *eng = @"en";
[languages replaceObjectAtIndex:0 withObject:chn];
[languages replaceObjectAtIndex:14 withObject:eng];
defs = (NSUserDefaults*)languages;
And I can change the content of NSUserDefaults defs. But it doesn't have an effect on applelanguage. I need to store the first object of defs as the current language.
Looks like you're trying to set the current language for the user.
That's not supported in the iOS sdk; the user must do this herself in settings.
If you explain your motivation for trying to do this, perhaps I could offer a solution.
You are probably trying to do this
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *languages = [defaults objectForKey:@"AppleLanguages"];
languages = [[languages mutableCopy] autorelease];
[languages replaceObjectAtIndex:0 withObject:@"zh-Hans"];
[languages replaceObjectAtIndex:14 withObject:@"en"]
[defaults setObject:languages forKey:@"AppleLanguages"];
But are you sure you want to impose a language on the user? He always has the choice of changing the language through the settings application.
You will also need to make sure you have set the localized string correctly. You should go through this document if you haven't already.
NSMutableArray *languages= [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSString *chn=@"zh-Hans";
NSString *eng=@"en";
[languages replaceObjectAtIndex:0 withObject:chn];
[languages replaceObjectAtIndex:14 withObject:eng];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSMutableArray *languagesAterChange= [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSLog(@"Lang:%@ | %@",[languagesAterChange objectAtIndex:0],[languagesAterChange objectAtIndex:14]);
There you go.
精彩评论