2016-12-01 52 views
1

我迅速2我所用的波紋管代碼獲取systemLocale:如何在swift 3中獲得systemLocaleCountryCode?

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

,但現在我在SWIFT 3,我收到了波紋管錯誤:

cannot call value of non function type locale

那麼一旦我改成了:

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

我收到另一個錯誤:

value of type Locale has no member objectForKey

有什麼問題?如何解決它?

+0

不'NSLocale.system.regionCode'返回預期值? – vadian

+0

相關:[如何使用Swift 3中的NSLocale獲取國家/地區代碼](http://stackoverflow.com/questions/39596238/how-to-get-country-code-using-nslocale-in-swift-3) –

+0

@ MartinR,謝謝 – david

回答

0

在Swift3 變化

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

使用像

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

謝謝你的回答,但現在我收到了這個錯誤:系統不可用請考慮使用用戶的語言環境或零取決於用例 – david

0

你可以試試這個:

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