How do you get the error code from self.speechRecognizer?.recognitionTask(with: self.recognitionRequest!, resultHandler: { (result, error)?

When using the ios Speech library SFSpeechRecognizer framework I want to specially handle some errors from recognitionTask. The problem is I can't figure out how to extract the error code. I've looked all around and I couldn't find an example or explanation in the doc on how to get at it.


The signature:

func recognitionTask(with request: SFSpeechRecognitionRequest, resultHandler: @escaping (SFSpeechRecognitionResult?, Error?) -> Void) -> SFSpeechRecognitionTask


Drilling down on Error says it's "protocol Error" which doesn't help very much.


When there is an error the local description show's there's a code:

2018-10-20 12:33:49.037515-0700 readsay[55123:12025377] error: Optional(Error Domain=kAFAssistantErrorDomain Code=209 "(null)")


The problem is the error doesn't seem to contain anything recognizable as an error code. There are option for flatMap, map, localDescription, debugDescription, mirror, unsafelyWrapped.


My Swift is not very swift. I assume the data is in the maps but there's no typing information I can find so that I can't extract them.

Can anyone please explain how to get to the error parameters?


Can anyone please explain how to get an error code out of the error?

Please show your code you have tried.

You can get the error code by bridging the Error to a NSError, like this:


        { result, error in
            let e = error as NSError
            print(e.code)
        }


You would, of course, check "e.domain" if you're testing for specific error codes in kAFAssistantErrorDomain.

How do you get the error code from self.speechRecognizer?.recognitionTask(with: self.recognitionRequest!, resultHandler: { (result, error)?
 
 
Q