如何在Swift 3中使用NSLocale获取国家/地区代码

dig*_*amu 42 ios nslocale swift3

能否请您就如何使用获得国家代码帮助NSLocaleSwift 3

这是我以前使用过的代码.

NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
Run Code Online (Sandbox Code Playgroud)

我可以在Swift 3中获得如下语言代码.

Locale.current.languageCode!
Run Code Online (Sandbox Code Playgroud)

我可以看到,获取languageCode是直截了当的,但countryCode属性不可用.

Woj*_*zki 65

您可以在Locale struct上使用regionCode属性.

Locale.current.regionCode
Run Code Online (Sandbox Code Playgroud)

它没有被记录为旧NSLocaleCountryCode构造的替代,但它看起来像是.以下代码检查所有已知语言环境的countryCodes,并将它们与regionCodes进行比较.它们完全相同.

public func ==(lhs: [String?], rhs: [String?]) -> Bool {
    guard lhs.count == rhs.count else { return false }

    for (left, right) in zip(lhs, rhs) {
        if left != right {
            return false
        }
    }

    return true
}

let newIdentifiers = Locale.availableIdentifiers
let newLocales = newIdentifiers.map { Locale(identifier: $0) }
let newCountryCodes = newLocales.map { $0.regionCode }

let oldIdentifiers = NSLocale.availableLocaleIdentifiers
newIdentifiers == oldIdentifiers // true

let oldLocales = oldIdentifiers.map { NSLocale(localeIdentifier: $0) }
let oldLocalesConverted = oldLocales.map { $0 as Locale }
newLocales == oldLocalesConverted // true

let oldComponents = oldIdentifiers.map { NSLocale.components(fromLocaleIdentifier: $0) }
let oldCountryCodes = oldComponents.map { $0[NSLocale.Key.countryCode.rawValue] }
newCountryCodes == oldCountryCodes // true
Run Code Online (Sandbox Code Playgroud)


Mar*_*n R 57

有关 更简单的解决方案,请参阅Wojciech N.的答案!


与在NSLocale Swift 3中类似,您必须将叠加类型转换Locale回其基础对应类型NSLocale以检索国家/地区代码:

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}
Run Code Online (Sandbox Code Playgroud)

  • 无需强制转换,只需使用“Locale.regionCode”即可,它们是相同的,请检查我的答案。/sf/answers/4231536361/ (2认同)

Mih*_*scu 7

NSLocale.countryCode是相同的Locale.regionCode

这是来自苹果的实现Locale语言环境.swift

    /// Returns the region code of the locale, or nil if it has none.
    ///
    /// For example, for the locale "zh-Hant-HK", returns "HK".
    public var regionCode: String? {
        // n.b. this is called countryCode in ObjC
        if let result = _wrapped.object(forKey: .countryCode) as? String {
            if result.isEmpty {
                return nil
            } else {
                return result
            }
        } else {
            return nil
        }
    }
Run Code Online (Sandbox Code Playgroud)


nev*_*ing 6

如果您确定使用的NSLocaleLocale实例而不是实例,则可以使用该countryCode属性:

let locale: NSLocale = NSLocale.current as NSLocale
let country: String? = locale.countryCode
print(country ?? "no country")
// > Prints "IE" or some other country code
Run Code Online (Sandbox Code Playgroud)

如果您尝试countryCode与 Swift一起使用,LocaleXcode 会给您一个错误,并建议您regionCode改用:

let swiftLocale: Locale = Locale.current
let swiftCountry: String? = swiftLocale.countryCode
// > Error "countryCode' is unavailable: use regionCode instead"
Run Code Online (Sandbox Code Playgroud)