本文介绍了如何在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获取国家/地区代码
发布评论