amagain
amagain

Reputation: 2072

Value of type 'String' has no member 'substringToIndex' not resolved

I am trying to grab currentLanguage of a device using the below function. But there has been an issue with substring and I don't seem to understand as previous answers have such simple solution of importing Foundation to the file, which didn't work for me.

class func currentLanguage() -> String
{
    let str = "en-US"       
    if let indexOfDash = str.characters.index(of: "-")
    {
        let langCode = str.substringToIndex(indexOfDash)

        return langCode
    }
}

In addition, What can be the best approach to get current language?

Upvotes: 0

Views: 926

Answers (1)

Artem Novichkov
Artem Novichkov

Reputation: 2341

You need to use

let langCode = str.substring(to: indexOfDash)

And you can get current language like that:

let pre = NSLocale.preferredLanguages[0]

Upvotes: 3

Related Questions