可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
Swift 4.2
let languageList = Locale.isoLanguageCodes.compactMap { Locale.current.localizedString(forLanguageCode: $0) }
let countryList = Locale.isoRegionCodes.compactMap { Locale.current.localizedString(forRegionCode: $0) }
回答5:
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)"
}
回答6:
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)"
}
回答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:
Swift You can easily retrieve countries name and their flags emoji.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var countriesData = [(name: String, flag: String)]()
for code in NSLocale.isoCountryCodes {
let flag = String.emojiFlag(for: code)
let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
if let name = NSLocale(localeIdentifier: "en_UK").displayName(forKey: NSLocale.Key.identifier, value: id) {
countriesData.append((name: name, flag: flag!))
}else{
//"Country not found for code: \(code)"
}
}
print(countriesData)
}
}
extension String {
static func emojiFlag(for countryCode: String) -> String! {
func isLowercaseASCIIScalar(_ scalar: Unicode.Scalar) -> Bool {
return scalar.value >= 0x61 && scalar.value <= 0x7A
}
func regionalIndicatorSymbol(for scalar: Unicode.Scalar) -> Unicode.Scalar {
precondition(isLowercaseASCIIScalar(scalar))
// 0x1F1E6 marks the start of the Regional Indicator Symbol range and corresponds to 'A'
// 0x61 marks the start of the lowercase ASCII alphabet: 'a'
return Unicode.Scalar(scalar.value + (0x1F1E6 - 0x61))!
}
let lowercasedCode = countryCode.lowercased()
guard lowercasedCode.count == 2 else { return nil }
guard lowercasedCode.unicodeScalars.reduce(true, { accum, scalar in accum && isLowercaseASCIIScalar(scalar) }) else { return nil }
let indicatorSymbols = lowercasedCode.unicodeScalars.map({ regionalIndicatorSymbol(for: $0) })
return String(indicatorSymbols.map({ Character($0) }))
}
}
Result:
回答10:
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
}
}
回答11:
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 }