NSLocaleCountryCode returns nil
Asked Answered
T

2

15

I have a bugreport that states a crash in the following line, where client is an instance of NSMutableDictionary

[client setObject:[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode] forKey:@"country"];

My guess is, that NSLocaleCountryCode returns nil in this line, which leads to adding a nil object to an NSDictionary which would lead to a crash. The question is, has anybody experienced an issue like this before? Are there any reasons NSLocaleCountryCode could be nil for the currentLocale? The documentation doesn't say anything about returning a nil value and I thought this would always return a valid country.

Best regards, Michael

Tegucigalpa answered 4/3, 2013 at 13:15 Comment(2)
Was this happening on an actual device or in testing? If it only happens during testing it might be what @olejnjak mentions below.Demonstrative
This happened on multiple real devices in production.Tegucigalpa
C
11

There are others who have experienced this issue, when NSLocale is the "system locale". You need to be more defensive in your coding, by either not populating the key if it's nil or using [NSNull null]:

NSLocale *locale = [NSLocale currentLocale];
NSString *country = [locale objectForKey:NSLocaleCountryCode];
if (country != nil)
{
    [client setObject:country forKey:@"country"];
}
Countersign answered 4/3, 2013 at 13:20 Comment(7)
Do you have any information under which circumstances the currentLocal will be equals the systemLocale?Tegucigalpa
No I don't but its clear that sometimes the country code isn't always populated and so needs to be optional in your final dictionary.Countersign
We have experience on the same issue from lot of our users. They are encountering this issue after updating our app. have you ever heard the currentLocale can suddenly change behavior as a consequence of an app update from the app store?Dieldrin
I had the same issue here, and I have no idea on what's causing the currentLocale not to have a country code.Tapis
I had a customer experiencing a crash for the same reason. The defensive coding above fixes the crash. But what I afterwards learned from the customer is that the iPad had General > International > Region Format set to "English", however, when she tapped to change the Region Format, "English" was no longer an option. This is under iOS 6.1.3. I don't think "English" should be an option, and my guess is, that Apple made a change at some point, eliminating "English" as an option, leaving the value on the iPad, and returning nil for NSLocaleCountryCode.Epispastic
This will repro the nil country code: Settings > General > International > Region Format > EsperantoFiner
@Finer genius.. how can you spot Esperanto! Thanks for highlighting it!Comminute
S
17

My case was that for debug purposes I had selected custom language in run scheme in Xcode. I had EN, not system language.

Xcode scheme language

Swordfish answered 14/9, 2015 at 15:56 Comment(0)
C
11

There are others who have experienced this issue, when NSLocale is the "system locale". You need to be more defensive in your coding, by either not populating the key if it's nil or using [NSNull null]:

NSLocale *locale = [NSLocale currentLocale];
NSString *country = [locale objectForKey:NSLocaleCountryCode];
if (country != nil)
{
    [client setObject:country forKey:@"country"];
}
Countersign answered 4/3, 2013 at 13:20 Comment(7)
Do you have any information under which circumstances the currentLocal will be equals the systemLocale?Tegucigalpa
No I don't but its clear that sometimes the country code isn't always populated and so needs to be optional in your final dictionary.Countersign
We have experience on the same issue from lot of our users. They are encountering this issue after updating our app. have you ever heard the currentLocale can suddenly change behavior as a consequence of an app update from the app store?Dieldrin
I had the same issue here, and I have no idea on what's causing the currentLocale not to have a country code.Tapis
I had a customer experiencing a crash for the same reason. The defensive coding above fixes the crash. But what I afterwards learned from the customer is that the iPad had General > International > Region Format set to "English", however, when she tapped to change the Region Format, "English" was no longer an option. This is under iOS 6.1.3. I don't think "English" should be an option, and my guess is, that Apple made a change at some point, eliminating "English" as an option, leaving the value on the iPad, and returning nil for NSLocaleCountryCode.Epispastic
This will repro the nil country code: Settings > General > International > Region Format > EsperantoFiner
@Finer genius.. how can you spot Esperanto! Thanks for highlighting it!Comminute

© 2022 - 2024 — McMap. All rights reserved.