Swift - Get list of countries
Asked Answered
B

3

6

How can I get an array with all countries names in Swift? I've tried to convert the code I had in Objective-C, which was this:

if (!pickerCountriesIsShown) {
    NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]];

    for (NSString *countryCode in [NSLocale ISOCountryCodes])
    {
        NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
        NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
        [countries addObject: country];
    }

And in Swift I can't pass from here:

        if (!countriesPickerShown) {
        var countries: NSMutableArray = NSMutableArray()
        countries = NSMutableArray.arrayWithCapacity((NSLocale.ISOCountryCodes).count) // Here gives the Error. It marks NSLocale.ISOCountryCodes and .count

Does anyone of you know about this?

Thanks

Beirut answered 19/6, 2014 at 20:31 Comment(0)
G
3

First of all ISOCountryCodes requires argument parenthesis so instead it would be ISOCountryCodes(). Second, you dont need parenthesis around NSLocale and ISOCountryCodes(). Also, arrayWithCapacity is deprecated meaning it is removed from the language. A working version of this would be somewhat like this

if (!countriesPickerShown) {
    var countries = NSMutableArray()
    countries = NSMutableArray(capacity: (NSLocale.ISOCountryCodes().count))
}
Gilbertogilbertson answered 19/6, 2014 at 20:45 Comment(1)
I am getting zero elements in array!Jorgan
K
6

Here's a Swift extension to NSLocale that returns an array of Swift-friendly Locale structs with the country names and country codes. It could easily be extended to include other country data.

extension NSLocale {

    struct Locale {
        let countryCode: String
        let countryName: String
    }

    class func locales() -> [Locale] {

        var locales = [Locale]()
        for localeCode in NSLocale.ISOCountryCodes() {
            let countryName = NSLocale.systemLocale().displayNameForKey(NSLocaleCountryCode, value: localeCode)!
            let countryCode = localeCode as! String
            let locale = Locale(countryCode: countryCode, countryName: countryName)
            locales.append(locale)
        }

        return locales
    }

}

And then it's easy to get the array of countries like this:

for locale in NSLocale.locales() {
    println("\(locale.countryCode) - \(locale.countryName)")
}
Konrad answered 11/5, 2015 at 18:41 Comment(0)
G
3

First of all ISOCountryCodes requires argument parenthesis so instead it would be ISOCountryCodes(). Second, you dont need parenthesis around NSLocale and ISOCountryCodes(). Also, arrayWithCapacity is deprecated meaning it is removed from the language. A working version of this would be somewhat like this

if (!countriesPickerShown) {
    var countries = NSMutableArray()
    countries = NSMutableArray(capacity: (NSLocale.ISOCountryCodes().count))
}
Gilbertogilbertson answered 19/6, 2014 at 20:45 Comment(1)
I am getting zero elements in array!Jorgan
H
2

It is an Operation not a Property

if let codes = NSLocale.ISOCountryCodes() {
    println(codes)
}
Heterozygous answered 19/6, 2014 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.