从IOS中的国家/地区名称获取国家/地区代

时间:2012-10-01 10:48:30

标签: ios country-codes nslocale

我用英语从服务器检索国家/地区名称。例如,“西班牙”

我想要做的是,假设国家名称将以英文书写,请获取国家/地区代码。

我该怎么办?我发现从国家代码中获取国家名称非常简单,但我不知道如何进行相反的操作。

非常感谢。

10 个答案:

答案 0 :(得分:30)

杰夫的answer在这里提供了帮助,但略有补充。

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',这是西班牙的2个字母的国家代码。

答案 1 :(得分:6)

@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

  

“英国”

它将返回国家/地区代码,如下所示

  

“GB”

希望它可以帮到你们!

答案 2 :(得分:4)

在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
    }


}

获取数据:

strP2countrycode = NSLocale.locales1("PASS_COUNTRYNAME")

答案 3 :(得分:2)

在swift 3中,你只能使用

let currentLocale = NSLocale.current
var countries = NSLocale.isoCountryCodes
currentLocale.localizedString(forRegionCode: countries.first!)

答案 4 :(得分:1)

我的紧凑版本,包括仅针对英国国家/地区名称版本进行匹配的修复程序。

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) ?? ""
    }
}

答案 5 :(得分:1)

使用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
}
}

答案 6 :(得分:1)

Swift 3.0:

将国家/地区代码和国家/地区名称作为NS Dictionary -

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)

答案 7 :(得分:0)

因此,这个解决方案在上面的评论中得到的快速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}
 }
}

享受!

答案 8 :(得分:0)

这是一个可以在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`

答案 9 :(得分:0)

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")