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

2020-05-30 03:17发布

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.

11条回答
干净又极端
2楼-- · 2020-05-30 03:47

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)
查看更多
放荡不羁爱自由
3楼-- · 2020-05-30 03:50

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 }
查看更多
冷血范
4楼-- · 2020-05-30 03:51

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楼-- · 2020-05-30 03:53

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)
查看更多
手持菜刀,她持情操
6楼-- · 2020-05-30 03:55

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
}()
查看更多
唯我独甜
7楼-- · 2020-05-30 03:58

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
  }
}
查看更多
登录 后发表回答