24

サーバーから国名を英語で取得しています。たとえば、「スペイン」

私がやりたいのは、国名が英語で書かれると仮定して、国コードを取得することです。

私は何をすべきか?国コードから国名を取得するのは非常に簡単ですが、逆の操作を行う方法がわかりません。

どうもありがとう。

4

9 に答える 9

30

ジェフの答えは、わずかな追加を加えて、ここで役に立ちました。

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文字の国コードです。

于 2012-11-30T09:39:51.127 に答える
14

@DaxeshNagarソリューションのSwift4更新バージョン:

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」

それが皆さんのお役に立てば幸いです!

于 2018-02-14T15:28:50.860 に答える
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")
于 2015-08-19T10:59:15.927 に答える
2

英語の国名バージョンとのみ一致するように修正されたコンパクトバージョン。

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) ?? ""
    }
}
于 2015-09-09T00:11:34.650 に答える
2

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
}
}
于 2017-02-10T17:02:13.813 に答える
2

これは、Swift3以降で機能する簡単なソリューションです。OSのバージョンに応じて、これは約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`
于 2019-05-21T19:25:36.683 に答える
1

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)
于 2017-07-14T11:24:04.570 に答える
0

したがって、このソリューションは、上記のコメントから抜粋したSwift4でうまく機能します...

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

楽しい!

于 2018-02-15T21:32:21.943 に答える
0

スウィフト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")
于 2021-01-17T08:42:26.510 に答える