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

编程入门 行业动态 更新时间:2024-10-26 06:35:56
本文介绍了如何在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获取国家/地区代码

本文发布于:2023-11-26 21:57:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   地区   国家   如何在   NSLocale

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!