Antoine Antoine - 4 months ago 118
iOS Question

Swift and Xcode: How to have a default list of countries?

Hi I am new to Xcode and Swift.

I am tying to code an App where user can select a country from a list so that it adds a pin to a map.

Basically, it is just a map with pins.

Where could I get a list of default countries ? like a drop-down or something so that I do not need to hardcode all of the countries myself.

Then, I know the next question is big so I just expect some guidance:

Could someone give me any directions on how using the GPS coordinates of the country selected so that it places a pin to a map ?

Thanks you all for your help.

Answer

Try with this.

func counrtyNames() -> NSArray{

    var countryCodes = NSLocale.ISOCountryCodes()
    var countries:NSMutableArray = NSMutableArray()

    for countryCode  in countryCodes{
        let dictionary : NSDictionary = NSDictionary(object:countryCode, forKey:NSLocaleCountryCode)

        //get identifire of the counrty
        var identifier:NSString? = NSLocale.localeIdentifierFromComponents(dictionary as! [String : String])

        let locale = NSLocale.currentLocale()
        //get country name
        let country = locale.displayNameForKey(NSLocaleCountryCode, value : countryCode)//replace "NSLocaleIdentifier"  with "NSLocaleCountryCode" to get language name

        if country != nil {//check the country name is  not nil
            countries.addObject(country!)
        }
    }
    NSLog("\(countries)")
    return countries
}