How to get country code using NSLocale in Swift 3

前端 未结 6 986
予麋鹿
予麋鹿 2020-12-13 17:31

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

This is the previous code I have been using.

N         


        
6条回答
  •  轮回少年
    2020-12-13 18:01

    NSLocale.countryCode is the same as Locale.regionCode

    This is from 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
            }
        }
    

提交回复
热议问题