david david - 2 days ago 4
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

In Swift3 change to

let systemLocaleCountryCode = (NSLocale.system as NSLocale).object(forKey: .countryCode) as? String
Comments