How to get country code using NSLocale in Swift 3

前端 未结 6 966
予麋鹿
予麋鹿 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 17:57

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


    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:

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

提交回复
热议问题