且构网

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

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

更新时间:2023-02-21 08:34:05

请参阅 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)
}