1

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 ?

david
  • 3,310
  • 7
  • 36
  • 59

3 Answers3

2

use like

 if let systemLocaleCountryCode = (Locale.system as NSLocale).object(forKey: .countryCode) as? String {
print(systemLocaleCountryCode)
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Thank you for your answer, but now i received this error : system is unavailable consider using the user's locale or nil instead depending on use case – david Dec 01 '16 at 11:56
0

In Swift3 change to

let systemLocaleCountryCode = (NSLocale.system as NSLocale).object(forKey: .countryCode) as? String
Cruz
  • 2,602
  • 19
  • 29
0

You can try this:

if let systemLocaleCountryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String
    {
        print(systemLocaleCountryCode)
    }
Pragnesh Vitthani
  • 2,532
  • 20
  • 28