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

后端 未结 12 1622
日久生厌
日久生厌 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 03:58

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

提交回复
热议问题