How do I get a list of countries in Swift ios?

家住魔仙堡 提交于 2019-12-03 09:52:04
Ian

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

var countries: [String] = []

for code in NSLocale.isoCountryCodes  {
    let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
    let name = NSLocale(localeIdentifier: "en_UK").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
    countries.append(name)
}

print(countries)

SWIFT 3 and 4

var countries: [String] = []

for code in NSLocale.isoCountryCodes as [String] {
    let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
    let name = NSLocale(localeIdentifier: "en_UK").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
    countries.append(name)
}

print(countries)

Same as Ian's but shorter

let countries = NSLocale.ISOCountryCodes().map { (code:String) -> String in
  let id = NSLocale.localeIdentifierFromComponents([NSLocaleCountryCode: code])
  return NSLocale(localeIdentifier: "en_US").displayNameForKey(NSLocaleIdentifier, value: id) ?? "Country not found for code: \(code)"
}

print(countries)

Here is @vedo27 's answer in Swift 3

 let countries = NSLocale.isoCountryCodes.map { (code:String) -> String in
    let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
    return NSLocale(localeIdentifier: "en_US").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
}

also it is a good practise to localize the displayed name of countries. So in addition to vedo27 answer it would be:

let countriesArray = NSLocale.ISOCountryCodes().map { (code:String) -> String in
        let id = NSLocale.localeIdentifierFromComponents([NSLocaleCountryCode: code])
        let currentLocaleID = NSLocale.currentLocale().localeIdentifier
        return NSLocale(localeIdentifier: currentLocaleID).displayNameForKey(NSLocaleIdentifier, value: id) ?? "Country not found for code: \(code)"
    }

Swift 3 declared as a var:

var countries: [String] {
    let myLanguageId = "en" // in which language I want to show the list
    return NSLocale.isoCountryCodes.map {
        return NSLocale(localeIdentifier: myLanguageId).localizedString(forCountryCode: $0) ?? $0
    }
}

SWIFT 4

Here is an elegant way of doing it in Swift 4

var countries: [String] = {

    var arrayOfCountries: [String] = []

    for code in NSLocale.isoCountryCodes as [String] {
        let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
        let name = NSLocale(localeIdentifier: "en_UK").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
        arrayOfCountries.append(name)
    }

    return arrayOfCountries
}()

Swift 4.2

let languageList = Locale.isoLanguageCodes.compactMap { Locale.current.localizedString(forLanguageCode: $0) }
let countryList = Locale.isoRegionCodes.compactMap { Locale.current.localizedString(forRegionCode: $0) }

Alternatively, you can have it in swift 4 and localized by the system language.

import Foundation

struct Country {
  let name: String
  let code: String
}

final class CountryHelper {
  class func allCountries() -> [Country] {
    var countries = [Country]()

    for code in Locale.isoRegionCodes as [String] {
      if let name = Locale.autoupdatingCurrent.localizedString(forRegionCode: code) {
        countries.append(Country(name: name, code: code))
      }
    }

    return countries
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!