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

后端 未结 12 1585
日久生厌
日久生厌 2021-02-05 03:29

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 insid

相关标签:
12条回答
  • 2021-02-05 04:04

    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)
    
    0 讨论(0)
  • 2021-02-05 04:12

    Here is code for Swift 5.1 (well at least that works for me as a lot of functions has been renamed):

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

    array here is an [String]

    0 讨论(0)
  • 2021-02-05 04:20

    Swift 4.2

    let languageList = Locale.isoLanguageCodes.compactMap { Locale.current.localizedString(forLanguageCode: $0) }
    let countryList = Locale.isoRegionCodes.compactMap { Locale.current.localizedString(forRegionCode: $0) }
    
    0 讨论(0)
  • 2021-02-05 04:21

    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)"
    }
    
    0 讨论(0)
  • 2021-02-05 04:21

    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
        }
    }
    
    0 讨论(0)
  • 2021-02-05 04:21

    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
      }
    }
    
    0 讨论(0)
提交回复
热议问题