2016-12-01 3 views
1

i swift 2 Ich war immer die systemLocale den unten Code:Wie SystemLocaleCountryCode in Swift 3 erhalten?

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

aber ich jetzt in schnellen 3, i Faltenbalgbefestigung Fehler erhalte:

cannot call value of non function type locale

dann, sobald ich es geändert:

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

ich habe einen anderen Fehler erhalten:

value of type Locale has no member objectForKey

Was ist das Problem? Wie man es repariert ?

+0

Does 'NSLocale.system.regionCode' den erwarteten Wert zurückgeben? – vadian

+0

Related: [So erhalten Sie Ländercode mit NSLocale in Swift 3] (http://stackoverflow.com/questions/39596238/how-to-get-country-code-using-nslocale-in-swift-3) –

+0

@ MartinR, danke – david

Antwort

0

In swift3 Änderung

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

Verwendung wie

if let systemLocaleCountryCode = (Locale.system as NSLocale).object(forKey: .countryCode) as? String { 
print(systemLocaleCountryCode) 
} 
+0

Vielen Dank für Ihre Antwort, aber jetzt habe ich diesen Fehler: System ist nicht verfügbar, die Verwendung des Gebietsschemas oder Nil statt abhängig von Anwendungsfall – david

0

Sie können dies versuchen:

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