2012-10-01 500 views
19

从英语服务器中检索国家名称。例如,“西班牙”在IOS中从国家名称获取国家代码

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

我该怎么办?我发现从国家代码中获取国名很容易,但我不知道如何做相反的操作。

非常感谢。

+0

过得好从国家名称的代码?只是做相反:) – deanWombourne

+0

我希望这临客将帮助您http://stackoverflow.com/questions/502764/iphone-obtaining-a-list-of-countries-in-an-nsarray –

+0

香港专业教育学院已经签了,但我想是根据一个国家的名字,而不是国家 – pdrcabrod

回答

29

杰夫的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为西班牙写信国家代码。

+4

好吧,如果你知道这个国家的名字都是英文的,你当然应该不要用currentLocale的列表,而是一个固定的区域得到的只是一个COUNTRYCODE。 currentLocale可以使用任何语言。改为使用[NSLocale initWithLocaleIdentifier:@“en_UK”]。 – fishinear

+0

initWithLocaleIdentifier是一个实例方法,而不是一个类的方法,因此使用'[NSLocale页头] initWithLocaleIdentifier:@“en_UK”]',而不是 – pyrou

+0

只是为了确保我也要小写/大写的字典键。我们不知道什么是服务器返回(这是一个不同的问题,但至少可以最大限度地减少“显示名”虚弱) – surui

3

在斯威夫特,您将使用此代码

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

这里是Daxesh Negar代码在迅速3

extension NSLocale { 
    class func locales1(countryName1 : String) -> String { 
     let locales : String = "" 
     for localeCode in NSLocale.isoCountryCodes { 
      let countryName = Locale.current.regionCode 
      if countryName1.lowercased() == countryName?.lowercased() { 
       return localeCode 
      } 
     } 
     return locales 
    } 
} 
1

使用迅速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 
} 
} 
1

夫特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) 
2

在SWIFT 3你可以只使用

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

代码只答案是沮丧,因为他们没有解释他们是如何解决这个问题。请更新您的答案,以解释这个问题已经具有的其他已接受和已获得解答的答案是如何改进的。此外,这个问题已经5年了,您的努力将会得到最近没有答案的用户的赞赏。请复习[我如何写出一个好答案](https://stackoverflow.com/help/how-to-answer)。 – FluffyKitten

+1

正如你可以看到我的答案只有3行,比其他国际海事组织更好。这个问题首先在谷歌,所以我决定分享我的答案。我知道这个问题已经有5年了,但我有很多开发者有问题。如果您不同意我的意见,请删除。 –

0

这是如何在雨燕4.0做到这一点(在斯威夫特3.2也可以)。

NSLocale.isoCountryCodes.forEach { (code) in 
    print(code) 
    let name = NSLocale(localeIdentifier: "zh_CN").displayName(forKey: NSLocale.Key.countryCode, value: code) 
    print(name) 
} 
2

@Daxesh的斯威夫特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”

希望它可以帮助你们这将返回国家代码!

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

享受!