我正在尝试使用以下函数获取设备的当前语言。但是子字符串有问题,我似乎不明白,因为以前的答案有将 Foundation 导入文件的简单解决方案,这对我不起作用。
class func currentLanguage() -> String
{
let str = "en-US"
if let indexOfDash = str.characters.index(of: "-")
{
let langCode = str.substringToIndex(indexOfDash)
return langCode
}
}
此外,获取当前语言的最佳方法是什么?
最佳答案
你需要使用
let langCode = str.substring(to: indexOfDash)
你可以像这样获取当前语言:
let pre = NSLocale.preferredLanguages[0]
关于ios - 类型 'String' 的值没有成员 'substringToIndex' 未解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40945842/