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

余生颓废 提交于 2020-01-01 03:59:05

问题


I've already seen two similar questions to mine, but the answers for those questions do not work for me. I have an old project with a list of countries manually typed out inside a set of square brackets.

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

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


回答1:


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)



回答2:


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)



回答3:


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)



回答4:


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)"
}



回答5:


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)"
    }



回答6:


Swift 4.2

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



回答7:


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
    }
}



回答8:


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
}()



回答9:


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
  }
}



回答10:


Update to vedo27's answer but in Swift 5:

let countries : [String] = 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 for having a sorted array for countries in Alphabetical order add this below in your order after fetching countries list

let sortedcountries = countries.sorted { $0 < $1 }


来源:https://stackoverflow.com/questions/27875463/how-do-i-get-a-list-of-countries-in-swift-ios

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