如何获得Swift ios中的国家/地区列表?

问题描述 投票:18回答:9

我已经看过两个与我相似的问题,但这些问题的答案对我不起作用。我有一个旧项目,其中包含在一组方括号内手动输入的国家/地区列表。

我可以在我的pickerView中轻松使用它,但我想知道是否有更有效的方法来做到这一点?

我将使用UIPickerView中的国家/地区列表。

cocoa-touch swift nsarray uipickerview nslocale
9个回答
29
投票

你可以使用NSLocale类的isoCountryCodes获得一个国家列表,它返回一个[String]数组。从那里,您可以使用NSLocaledisplayName(forKey:)方法获得国家名称。它看起来像这样:

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)

16
投票

SWIFT 3和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)

8
投票

与Ian相同,但更短

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
投票

这是@ vedo27在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)"
}

2
投票

将本地化的国家名称本地化也是一种很好的做法。所以除了vedo27的回答,它将是:

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

2
投票

Swift 3宣称为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
    }
}

2
投票

SWIFT 4

这是在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
}()

2
投票

Swift 4.2

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

1
投票

或者,您可以在swift 4中使用它并使用系统语言进行本地化。

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
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.