我使用以下代码作为代码和国家/地区名称,但它返回 null。
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
NSLog(@"Language : %@ Lang2 : %@",[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0],[[NSLocale preferredLanguages] objectAtIndex:0]);
NSLog(@"Locale:%@ Code:%@ Name%@:", locale, countryCode, country);
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
fr,
de,
nl,
ja,
es,
tr,
it,
pl,
pt,
);
日志:区域设置:<__NSCFLocale:0x7fbef943f810> 代码:(null) 名称(null)
语言:nl Lang2:nl
在设备和模拟器上都测试过。我不知道这里发生了什么。它应该可以正常工作。如果你们有任何想法请告诉我。我正在使用 xcode 6.4。
最佳答案
终于找到了窃听器。
我在另一个测试项目中测试了上面的代码并且工作正常但在我当前的项目中却不行。 所以我使用了开发工具 FileMerge 并比较了两者,发现下面一行有所不同。(xcshareddata->xcschemes->****.xcscheme)。
我不知道 language = "nl"是如何到达那里的,可能是由于错误的合并,但回滚和重新合并也没有用。
手动删除线解决了我的幽灵问题:)
关于ios - NSLocale 国家代码和名称为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31651015/