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

后端 未结 12 1576
日久生厌
日久生厌 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:59

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

提交回复
热议问题