我正在从英语服务器检索国家名称,例如“西班牙”。
我想做的是,在假定国家名称将以英文编写的情况下,获取国家代码。
我该怎么做?我发现从国家代码获取国家名称相当容易,但我不知道如何进行相反的操作。
非常感谢。
我正在从英语服务器检索国家名称,例如“西班牙”。
我想做的是,在假定国家名称将以英文编写的情况下,获取国家代码。
我该怎么做?我发现从国家代码获取国家名称相当容易,但我不知道如何进行相反的操作。
非常感谢。
Jef的答案在这里有所帮助,稍加补充即可。
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];
现在通过'codeForCountryDictionary'使用您需要代码的国家名称,例如:
NSLog(@"%@",[codeForCountryDictionary objectForKey:@"Spain"]);
这将得出结果为“ES”,它是西班牙的两个字母国家代码。
[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"]
。 - pyrou@Daxesh Nagar的解决方案的Swift 4更新版本:
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
}
将此输入作为 fullCountryName
"United Kingdom"
它将返回以下国家代码
"GB"
希望能对你们有所帮助!
fullCountryName
是 "Estados Unidos",有什么建议吗?好的解决方案,谢谢! - ArielSDextension 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
}
}
获取数据:
strP2countrycode = NSLocale.locales1("PASS_COUNTRYNAME")
使用 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
}
}
Locale
? - rmaddy我简化了版本并修复了只匹配英文国家名称问题。
extension NSLocale
{
class func localeForCountry(countryName : String) -> String?
{
return NSLocale.ISOCountryCodes().first{self.countryNameFromLocaleCode($0 as! String) == countryName} as? String
}
private class func countryNameFromLocaleCode(localeCode : String) -> String
{
return NSLocale(localeIdentifier: "en_UK").displayNameForKey(NSLocaleCountryCode, value: localeCode) ?? ""
}
}
en_US_POSIX
区域设置。 - rmaddy这里是一个简单的解决方案,适用于Swift 3或更高版本。根据您的操作系统版本,此方案支持大约256个国家名称和代码。
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
})
}
}
正确使用此功能的诀窍在于了解您想要转换为标准2字母ISO国家代码的国家名称的语言。
如果您知道国家名称是英文的,则必须将其用于设置为英文的区域设置。在这种情况下最好使用特殊的区域设置en_US_POSIX
。
let locale = Locale(identifier: "en_US_POSIX")
print(locale.isoCode(for: "Spain")) // result is `ES`
let locale = Locale(identifier: "es")
print(locale.isoCode(for: "España")) // result is `ES`
Swift 3.0 :
获取国家代码和国家名称作为NS字典 -
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)
NS
类都存在? - rmaddy所以这个解决方案在Swift 4中很好用,取自上面的评论...
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{$0.countryName.localizedCaseInsensitiveCompare($1.countryName) == ComparisonResult.orderedAscending}
}
}
享受!
Swift 5
extension Locale {
func countryCode(by countryName: String) -> String? {
return Locale.isoRegionCodes.first(where: { code -> Bool in
guard let localizedCountryName = localizedString(forRegionCode: code) else {
return false
}
return localizedCountryName.lowercased() == countryName.lowercased()
})
}
}
使用方法:
let locale = Locale(identifier: "en_US")
let countryCode = locale.countryCode(by: "Russia")