david david - 1 year ago 130
iOS Question

How to get systemLocaleCountryCode in swift 3?

i swift 2 i was getting the systemLocale using the bellow code:

let systemLocaleCountryCode = NSLocale.systemLocale().objectForKey(NSLocaleCountryCode) as? String

but now i in swift 3 , i am receiving the bellow error :

cannot call value of non function type locale

then once i changed it to :

let systemLocaleCountryCode = NSLocale.systemLocale.objectForKey(NSLocaleCountryCode) as? String

I've received another error:

value of type Locale has no member objectForKey

what's the problem ? How to fix it ?

Answer Source

In Swift3 change to

let systemLocaleCountryCode = (NSLocale.system as NSLocale).object(forKey: .countryCode) as? String
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download