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

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

提交回复
热议问题