如何在Swift 3中使用NSLocale获取国家/地区代码 [英] How to get country code using NSLocale in Swift 3
本文介绍了如何在Swift 3中使用NSLocale获取国家/地区代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问如何在 Swift 3
中使用 NSLocale
获取国家/地区代码?
Could you please help on how to get country code using NSLocale
in Swift 3
?
这是我以前使用过的代码。
This is the previous code I have been using.
NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
我可以在Swift 3中获得如下语言代码。
I can get Language Code as below in Swift 3.
Locale.current.languageCode!
正如我所看到的,提取languageCode是直截了当的,但countryCode属性不可用。
As I can see, fetching languageCode is straight forward but countryCode property is not available.
推荐答案
请参阅 Wojciech N.回答
以获得更简单的解决方案!
与 NSLocale Swift 3 ,您必须投射叠加类型区域设置
回到
基金会对应 NSLocale
以便检索国家代码:
Similarly as in NSLocale Swift 3, you have to cast the overlay type Locale
back to its
Foundation counterpart NSLocale
in order to retrieve the country code:
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
这篇关于如何在Swift 3中使用NSLocale获取国家/地区代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文