如何获取Swift ios中的国家/地区列表? [英] How do I get a list of countries in Swift ios?

查看:1449
本文介绍了如何获取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中的国家/地区列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆