0

I ran into something I don't understand. I have set a language selection for the application in the settings menu of the phone. I have the delegate method

- (void)defaultsChanged:(NSNotification *)notif 

in my AppDelegate. I read the new parameter of the selected language and try to change the language with

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];

Unfortunatly this line always gives me a EXC_BAD_ACCESS error. I don't understand why.

I tried different ways given on StackOverflow to change Localization at runtime but they only work for texts in the application. I also have images that are localized and they only updates when the language is changed in the NSUserDefaults.

I need some advices here.

Thank you !

DEIONaLiMs
  • 162
  • 1
  • 11
  • Can you add your stack trace / console log? – deanWombourne Dec 06 '11 at 14:32
  • Did you enable [zombies](http://stackoverflow.com/questions/2190227/how-do-i-set-nszombieenabled-in-xcode-4) – rckoenes Dec 06 '11 at 14:36
  • @deanWombourne Actually there is nothing on the console and no stack trace – DEIONaLiMs Dec 06 '11 at 14:50
  • @rckoenes I don't know what are zombies (other than... well... TV) and I don't know how to enable them. – DEIONaLiMs Dec 06 '11 at 14:52
  • @DEIONaLiMs click on the word zombies it will tell you what they are and how to use them. – rckoenes Dec 06 '11 at 15:04
  • 1
    I think I know what's going on. The NSNotification* of (void)defaultsChanged:(NSNotification *)notif is NSUserDefaultsDidChangeNotification. Calling [[NSUserDefaults standardUserDefaults] setObject:obj forKey:key] will then trigger defaultsChanged... We then have a loop and a... well... stackoverflow :) What d'you think guys ? – DEIONaLiMs Dec 06 '11 at 15:05
  • @rckoenes Ah nice. Thanks for the tips. – DEIONaLiMs Dec 06 '11 at 15:15

1 Answers1

0

It does seems to me that making changes to NSUserDefaults inside the defaultsChanged handler is not a good idea. As per your own comment it causes recursion.

If you do need to change the NSUserDefaults inside your defaultsChanged, a workaround would be to add a boolean flag that says you're making changes so that you know not to do anything when you are called in response to your own changes.

Perhaps you need a different storage method than NSUserDefaults for your state, such as a plist. I know that NSUserDefaults is really convenient to use, and that is the main reason people use (and sometimes misuse) it for all sorts of state that isn't actually user preferences, but it can cause problems due to all the bells and whistles it comes with, such as the support for NSUserDefaultsDidChangeNotification observers.

Clafou
  • 15,250
  • 7
  • 58
  • 89
  • Thanks for the additional infos. Even with the boolean-flag-workaround this did not solve my problem. Is there even a way to switch between localized files (like images for my case) at runtime ? Again, the solutions to switch between differents LocalizedStrings given here work like a charm. But it won't take the right localized image. I'm pretty sure the application must be restarted for this to take effect. – DEIONaLiMs Dec 07 '11 at 15:47
  • Do you mean it still crashes? – Clafou Dec 07 '11 at 16:10
  • I think changing the language of localized images on the fly is a whole different question. If you're able to do it successfully for strings I'd imagine you could use the same approach for images. You could try posting a question explaining how you are doing it for strings with some code, and asking how to extend it to images. – Clafou Dec 07 '11 at 22:19