Ramon Vasconcelos Ramon Vasconcelos - 1 month ago 18
Swift Question

SFSpeechRecognizer init not accepting Locale

I'm trying to initialise an instance of

SFSpeechRecognizer
but I get the following error
Cannot convert value of type 'Locale' to expected argument type 'Locale'
which doesn't make any sense to me. Bellow is my initialisation code.

let speech = SFSpeechRecognizer(locale: Locale().initWithLanguageCode(languageCode: "en-US") as! Locale)

Answer

It should be:

if let speech = SFSpeechRecognizer(locale: Locale(identifier: "en-US")) {
     // ...
}

Use init(localeIdentifier:) for initialization.

let locale = Locale()

This causes:

enter image description here

Note that speech is an optional.

ALSO: here you can check iOS Locale Identifiers.

Hope this helped.