问题描述:

I would like to get a list of countries with it respective Language and currency.

First I created my class to store the information I need:

class Country: NSObject {

var localeID:String?

var name:String?

var code:String?

var currencyCode:String?

var currencySymbol:String?

var currencyName:String?

var languageCode:String?

var languageName:String?

}

Then I started the iteration over ISOCountryCodes with:

 let countriesISOCodes = NSLocale.ISOCountryCodes()

for country in countriesISOCodes {

// code here .....

}

Then I tried to get the country name inside for loop with :

 let anyLocale = NSLocale(localeIdentifier: country)

country.name = anyLocale.displayNameForKey(NSLocaleCountryCode, value : code)

Everything was great until I tried to get currencyCode, currencySymbol, currencyName, languageCode, languageName

I tried a bunch of combinations of NSLocale.objectForKey and NSLocale.displayNameForKey and nothing worked.

Do you know how I can get a list of countries using ISOCountryCode with respective currency and language information ?

网友答案:

Here's a list of the relevant keys and value types:

NSLocale component keys

You use the function objectForKey to retrieve the values.

网友答案:

If you check currentLocale() method, localeIdentifier is a string format like "en_CN". ISOCountryCode() method just return a list of country codes each is part of localeIdentifier. You may use NSLocale.availableLocaleIdentifiers() instead.

let countriesIdentifiers = NSLocale.availableLocaleIdentifiers()
for country in countriesIdentifiers {

let anyLocale = NSLocale(localeIdentifier: country)
// code here .....
}
相关阅读:
Top