2

I asked few days ago about how to get a list of countries in Swift, and you solved the first part of it (here: Swift - Get list of countries), but I am not able to get the final countries list because it gives an error:

var countries: NSMutableArray = NSMutableArray()
countries = NSMutableArray(capacity: (NSLocale.ISOCountryCodes().count))
for countryCode : AnyObject in NSLocale.ISOCountryCodes() {
  let dictionary : NSDictionary = NSDictionary(object:countryCode, forKey:NSLocaleCountryCode)
  let identifier : NSString     = NSLocale.localeIdentifierFromComponents(dictionary)
  // next line - fatal error: Can't unwrap Optional.None
  let country    : NSString     = 
    NSLocale.currentLocale().displayNameForKey(NSLocaleIdentifier, value: identifier)
  countries.addObject(country)
}
println(countries)

What does the "fatal error: Can't unwrap Optional.None" error mean? I've been trying and searching about it but I haven't find the solution yet :/

Million thanks :)

Community
  • 1
  • 1
rulilg
  • 1,604
  • 4
  • 15
  • 19

5 Answers5

2

this line is an Optional String

  let identifier : NSString = NSLocale.localeIdentifierFromComponents(dictionary)

replace with

  let identifier : NSString? = NSLocale.localeIdentifierFromComponents(dictionary)

and then

if identifier {
  let country : NSString = 
     NSLocale.currentLocale().displayNameForKey(NSLocaleIdentifier, value: identifier!)   
 countries.addObject(country)
}
shavik
  • 75
  • 1
  • 10
0

Try to this one :D

var countries : String[] = Array()
for countryCodes : AnyObject in NSLocale.ISOCountryCodes() {
        let dictionary : NSDictionary = NSDictionary(object:countryCodes, forKey:NSLocaleCountryCode)
        let identifier : NSString? = NSLocale.localeIdentifierFromComponents(dictionary)
        if identifier {
            let country : NSString = NSLocale.currentLocale().displayNameForKey(NSLocaleIdentifier, value: identifier!)
            countries.append(country)
        }
    }
println(countries)

i think this one code can help you get a list of countries :D

0

I face same problem with you, here is my code

for countryCodes : AnyObject in NSLocale.ISOCountryCodes() {
    let dictionary : Dictionary = [NSLocaleCountryCode: countryCodes]
    let identifier : String? = NSLocale.localeIdentifierFromComponents(dictionary)
    if let id = identifier {
        let country : String = NSLocale(localeIdentifier: "en_GB").displayNameForKey(NSLocaleIdentifier, value: id)
        countries.append(country)
    }
}

or change from NSLocale.currentLocale() to NSLocale(localeIdentifier: "en_GB")

Zoon Nooz
  • 5,836
  • 2
  • 22
  • 16
0

For swift3

let identifier = Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue:countryCode])
Den Jo
  • 413
  • 4
  • 10
0

try this:

(edited) using Locale, instead of NSLocale

func getCountriesList()->[String]{
    var countries = [String]()
    for code in Locale.isoRegionCodes {
        let id = Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
        if let name = Locale(identifier: id).localizedString(forRegionCode: code) {
            countries.append(name)
        }
    }
    return countries
}
Sanad Barjawi
  • 539
  • 4
  • 15
  • Don't use `NSLocale` in Swift, use native `Locale`. Then you can omit the entire `let id = ` line and replace the next line with `let name = Locale(identifier: "en").localizedString(forRegionCode: code) ?? "Country not found for code: \(code)"`. Further in `Locale` `isoCountryCodes` is `isoRegionCodes` and the bridge cast `as String` is redundant. – vadian Oct 24 '18 at 10:05