Search code examples
ioslocaleswift2

How to set app locale in swift2.0


I am probably missing something stupid, but I couldn't apply answers for the same question in Objective-C. Here is the thing:

I want to explicitly set the language/locale of the app. To do that I am using the code in didFinishLaunchingWithOptions as follows:

NSUserDefaults.standardUserDefaults().setObject("en-US", forKey: "AppleLanguages")
NSUserDefaults.standardUserDefaults().synchronize()

Evidently this code doesn't bring up any compiler errors as it relies on work with the dictionary but throws an exception during runtime as soon as GMSMapView is shown (this class does utilize the locale to define the language of address reverse geocoded from CLLocationCoordinate2D). The error immediately terminates the app and looks as shown below:

[Swift._NSContiguousString count]: unrecognized selector sent to instance 0x7b67ac60

Please, give an insight on what I am doing wrong with the value for the key, or prove me wrong in terms of mistake origin and set any other direction to investigate :)

Many thanks in advance!

Michael


Solution

  • Sorry for any disturbance, the issue was crystal clear! I missed the fact that a value for a key should be presented as an array. This results in the following code working as expected:

     NSUserDefaults.standardUserDefaults().setObject(["en-US"], forKey: "AppleLanguages")
     NSUserDefaults.standardUserDefaults().synchronize()