且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Swift 3 中使用 NSLocale 获取国家代码

更新时间:2023-02-21 08:38:23

参见 Wojciech N. 的回答寻求更简单的解决方案!

See Wojciech N.'s answer for a simpler solution!

NSLocale Swift 3类似,您必须转换覆盖类型Locale 回到它的基础对应 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)
}