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

时间:2021-10-14 07:30:45

Could you please help on how to get country code using NSLocale in Swift 3 ?

你能帮忙看看如何在Swift 3中使用NSLocale获取国家代码吗?

This is the previous code I have been using.

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

NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String

I can get Language Code as below in Swift 3.

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

Locale.current.languageCode!

As I can see, fetching languageCode is straight forward but countryCode property is not available.

正如我所看到的,获取languageCode是直截了当的,但countryCode属性不可用。

4 个解决方案

#1


45  

See Wojciech N.'s answer for a simpler solution!

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


Similarly as in NSLocale Swift 3, you have to cast the overlay type Locale back to its Foundation counterpart NSLocale in order to retrieve the country code:

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

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}

#2


51  

You can use regionCode property on Locale struct.

您可以在Locale结构上使用regionCode属性。

Locale.current.regionCode

It is not documented as a substitute for old NSLocaleCountryCode construct but it looks like it is. The following code checks countryCodes for all known locales and compares them with regionCodes. They are identical.

它没有被记录为旧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

#3


0  

If you make sure that you're using an NSLocale and not a Locale instance, you can use the countryCode property:

如果确保使用的是NSLocale而不是Locale实例,则可以使用countryCode属性:

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

If you try to use countryCode with a Swift Locale Xcode will give you an error with a suggestion to use regionCode instead:

如果您尝试将countryCode与Swift Locale一起使用Xcode会给您一个错误,建议您使用regionCode:

let swiftLocale: Locale = Locale.current
let swiftCountry: String? = swiftLocale.countryCode
// > Error "countryCode' is unavailable: use regionCode instead"

#4


-5  

print(NSLocale.current.regionCode)

#1


45  

See Wojciech N.'s answer for a simpler solution!

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


Similarly as in NSLocale Swift 3, you have to cast the overlay type Locale back to its Foundation counterpart NSLocale in order to retrieve the country code:

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

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}

#2


51  

You can use regionCode property on Locale struct.

您可以在Locale结构上使用regionCode属性。

Locale.current.regionCode

It is not documented as a substitute for old NSLocaleCountryCode construct but it looks like it is. The following code checks countryCodes for all known locales and compares them with regionCodes. They are identical.

它没有被记录为旧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

#3


0  

If you make sure that you're using an NSLocale and not a Locale instance, you can use the countryCode property:

如果确保使用的是NSLocale而不是Locale实例,则可以使用countryCode属性:

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

If you try to use countryCode with a Swift Locale Xcode will give you an error with a suggestion to use regionCode instead:

如果您尝试将countryCode与Swift Locale一起使用Xcode会给您一个错误,建议您使用regionCode:

let swiftLocale: Locale = Locale.current
let swiftCountry: String? = swiftLocale.countryCode
// > Error "countryCode' is unavailable: use regionCode instead"

#4


-5  

print(NSLocale.current.regionCode)