如何获取Swift ios中的国家/地区列表?

编程入门 行业动态 更新时间:2024-10-25 10:33:12
本文介绍了如何获取Swift ios中的国家/地区列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有两个类似的问题,他们的答案不适合我。我有一个旧项目,其中列出了一组手动输入的国家和一组方括号。

There are two of similar questions and their answers do not work for me. I have an old project with a list of countries manually typed out and inside a set of square brackets.

我可以很容易地在我的pickerView中使用这个,但我想知道是否有更有效的方法来做这个?

I can easily use this in my pickerView but I'm wondering if there is a more efficient way to do this?

我将使用UIPickerView中的国家/地区列表。

I will be using the list of countries in a UIPickerView.

推荐答案

NSLocale类的 ISOCountryCodes()方法返回一个 [AnyObject] [String] 。从那里,你通过使用NSLocale的 displayNameForKey 方法获得国家名称。它看起来像这样:

You can get a list of countries using the NSLocale class's ISOCountryCodes() method which returns an array of [AnyObject] which you cast as [String]. From there, you get the country name by using NSLocale's displayNameForKey method. It looks like this:

var countries: [String] = [] for code in NSLocale.ISOCountryCodes() as [String] { let id = NSLocale.localeIdentifierFromComponents([NSLocaleCountryCode: code]) let name = NSLocale(localeIdentifier: "en_UK").displayNameForKey(NSLocaleIdentifier, value: id) ?? "Country not found for code: \(code)" countries.append(name) } println(countries)

更多推荐

如何获取Swift ios中的国家/地区列表?

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

发布评论

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

>www.elefans.com

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