ios 从IOS中的国家/地区名称获取国家/地区代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12671829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get country code from country name in IOS
提问by pdrcabrod
Im retrieving a country name from a server in english. For example, "Spain"
我正在用英语从服务器中检索国家/地区名称。例如,“西班牙”
What I want to do is, assuming the country name is going to be written in english, get the country code.
我想要做的是,假设国家名称将用英文书写,获取国家代码。
What should I do? I′ve found getting the country name from the country code quite easy, but I′ve got no idea of how to do the opposite operation.
我该怎么办?我发现从国家代码中获取国家名称非常容易,但我不知道如何进行相反的操作。
Thanks a lot.
非常感谢。
回答by Breakpoint
Jef's answerhelped here, with slight additions.
杰夫的回答在这里有所帮助,并略有补充。
NSArray *countryCodes = [NSLocale ISOCountryCodes];
NSMutableArray *countries = [NSMutableArray arrayWithCapacity:[countryCodes count]];
for (NSString *countryCode in countryCodes)
{
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
NSString *country = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"] displayNameForKey: NSLocaleIdentifier value: identifier];
[countries addObject: country];
}
NSDictionary *codeForCountryDictionary = [[NSDictionary alloc] initWithObjects:countryCodes forKeys:countries];
Now go through the 'codeForCountryDictionary' with the name of the country for which you require the code, for example,
现在通过“codeForCountryDictionary”输入您需要代码的国家/地区的名称,例如,
NSLog(@"%@",[codeForCountryDictionary objectForKey:@"Spain"]);
would yield the result as 'ES', which is the 2 letter country code for spain.
将产生的结果为“ES”,这是西班牙的 2 个字母国家/地区代码。
回答by GuilhermeM
A Swift 4updated version of @Daxesh Nagarsolution:
一个斯威夫特4更新版本@Daxesh Nagar的解决方案:
private func locale(for fullCountryName : String) -> String {
var locales : String = ""
for localeCode in NSLocale.isoCountryCodes {
let identifier = NSLocale(localeIdentifier: localeCode)
let countryName = identifier.displayName(forKey: NSLocale.Key.countryCode, value: localeCode)
if fullCountryName.lowercased() == countryName?.lowercased() {
return localeCode as! String
}
}
return locales
}
For this Input as fullCountryName
对于此输入为fullCountryName
"United Kingdom"
“英国”
It will return the country code as follows
它将返回国家代码如下
"GB"
“国标”
Hope it helps you guys!
希望对大家有帮助!
回答by Daxesh Nagar
In the Swift you will use this code
在 Swift 中,您将使用此代码
extension NSLocale {
class func locales1(countryName1 : String) -> String {
var locales : String = ""
for localeCode in NSLocale.ISOCountryCodes() {
let countryName = NSLocale.systemLocale().displayNameForKey(NSLocaleCountryCode, value: localeCode)!
if countryName1.lowercaseString == countryName.lowercaseString {
return localeCode as! String
}
}
return locales
}
}
Get the data:
获取数据:
strP2countrycode = NSLocale.locales1("PASS_COUNTRYNAME")
回答by Fawkes
My compact version including the fix for matching only against english country names version.
我的紧凑版本包括仅与英文国家名称版本匹配的修复程序。
extension NSLocale
{
class func localeForCountry(countryName : String) -> String?
{
return NSLocale.ISOCountryCodes().first{self.countryNameFromLocaleCode(let currentLocale = NSLocale.current
var countries = NSLocale.isoCountryCodes
currentLocale.localizedString(forRegionCode: countries.first!)
as! String) == countryName} as? String
}
private class func countryNameFromLocaleCode(localeCode : String) -> String
{
return NSLocale(localeIdentifier: "en_UK").displayNameForKey(NSLocaleCountryCode, value: localeCode) ?? ""
}
}
回答by Piotr Badura
In swift 3 you can just only use
在 swift 3 中你只能使用
extension NSLocale {
class func locales1(countryName1 : String) -> String {
let locales : String = ""
for localeCode in NSLocale.isoCountryCodes {
let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode)
if countryName1.lowercased() == countryName?.lowercased() {
return localeCode
}
}
return locales
}
}
回答by Robert Varga
Using swift 3 (some of the answers above are missing a piece)
使用 swift 3(上面的一些答案遗漏了一部分)
let countryCodes: [AnyObject] = NSLocale.isoCountryCodes as [AnyObject]
let countries: NSMutableArray = NSMutableArray(capacity: countryCodes.count)
for countryCode in countryCodes {
let identifier: String = NSLocale.localeIdentifier(fromComponents: NSDictionary(object: countryCode, forKey: NSLocale.Key.countryCode as NSCopying) as! [String : String])
let country: String = NSLocale(localeIdentifier: "en_US").displayName(forKey: NSLocale.Key.identifier, value: identifier)!
countries.add(country)
}
let codeForCountryDictionary: [NSObject : AnyObject] = NSDictionary(objects: countryCodes, forKeys: countries as! [NSCopying]) as [NSObject : AnyObject]
print(codeForCountryDictionary)
回答by A.G
Swift 3.0 :
斯威夫特 3.0:
Getting the country code and country name as NS Dictionary-
获取国家代码和国家名称作为 NS 字典-
extension Locale {
func isoCode(for countryName: String) -> String? {
return Locale.isoRegionCodes.first(where: { (code) -> Bool in
localizedString(forRegionCode: code)?.compare(countryName, options: [.caseInsensitive, .diacriticInsensitive]) == .orderedSame
})
}
}
回答by rmaddy
Here is a simple solution that works in Swift 3 or later. Depending on your OS version, this supports roughly 256 country names and codes.
这是一个适用于 Swift 3 或更高版本的简单解决方案。根据您的操作系统版本,这支持大约 256 个国家/地区名称和代码。
let locale = Locale(identifier: "en_US_POSIX")
print(locale.isoCode(for: "Spain")) // result is `ES`
The trick to using this correctly is knowing the language of the country names you wish to convert to the standard 2-letter ISO country code.
正确使用它的技巧是了解您希望转换为标准 2 字母 ISO 国家代码的国家名称的语言。
If you know the country names are in English, you must use this on a locale set to English. It would be best to use the special locale of en_US_POSIX
in such a case.
如果您知道国家/地区名称是英语,则必须在设置为英语的语言环境中使用它。en_US_POSIX
在这种情况下最好使用特殊的语言环境。
let locale = Locale(identifier: "es")
print(locale.isoCode(for: "Espa?a")) // result is `ES`
If you have country names in Spanish, then be sure to use a Spanish locale:
如果您有西班牙语的国家/地区名称,请确保使用西班牙语语言环境:
extension NSLocale {
struct Locale {
let countryCode: String
let countryName: String
}
class func locales() -> [Locale] {
var locales = [Locale]()
for localeCode in NSLocale.isoCountryCodes {
let identifier = NSLocale(localeIdentifier: localeCode)
let countryName = identifier.displayName(forKey: NSLocale.Key.countryCode, value: localeCode)!
let countryCode = localeCode
let locale = Locale(countryCode: countryCode, countryName: countryName)
locales.append(locale)
}
return locales.sorted{##代码##.countryName.localizedCaseInsensitiveCompare(.countryName) == ComparisonResult.orderedAscending}
}
}
回答by Pablo Segura
So this solution works well in swift 4 taken from comments above...
所以这个解决方案在 swift 4 中运行良好,来自上面的评论......
##代码##enjoy!
请享用!