4

In most cases, setting a delegate is as simple as implementing the delegate protocol in a class and declaring an instance of that class as the delegate for an instance of whatever you're using.

I actually used this same basic concept for the SFSpeechRecognizer which belongs to the same speech framework in my code. (Pseudocode example):

class myViewControllerClass: SFSpeechRecognizerDelegate{

let mySpeechRecognizer = SFSpeechRecognizer(...)

viewDidLoad(){
mySpeechRecognizer.delegate = self
}
...

//SFSpeechRecognizerDelegate Functions here
...

}  
//This works as expected, woo!

However, it seems that SFSpeechRecognitionTask has no delegate property which can be set. I tried implementing the 'SFSpeechRecognitionTaskDelegate' protocol in my class in hopes that it would just magically work. However the delegate functions don't seem to ever be called. Which kind of makes sense, because it has no way of knowing that my view controller should be the delegate so why would it!?

The apple documentation covers the protocol itself and how to use it:

https://developer.apple.com/reference/speech/sfspeechrecognitiontaskdelegate

But the documentation for the task itself doesn't identify any delegate property:

https://developer.apple.com/reference/speech/sfspeechrecognitiontask

Also for reference here's the SFSpeechRecognizer documentation which has the protocol AND identifies a delegate property as you'd expect:

https://developer.apple.com/reference/speech/sfspeechrecognizer

Is there some alternative way I'm supposed to be setting the delegate for an SFSpeechRecognitionTask? Or is it handled in some completely different way?

Bebhead
  • 209
  • 3
  • 14

2 Answers2

7

In SFSpeechRecognizer there is a method

func recognitionTask(with request: SFSpeechRecognitionRequest, 
        delegate: SFSpeechRecognitionTaskDelegate) -> SFSpeechRecognizerTask

Where you can pass on the delegate for the SFSpeechRecognizerTask.

TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
  • 2
    Ah yes that does solve the delegate problem, but leaves me with another question which may be a silly one... I'm currently using the other recognitionTask method on SpeechRecognizer: `func recognitionTask(with: SFSpeechRecognitionRequest, resultHandler: @escaping (SFSpeechRecognitionResult?, Error?) -> Void)` and using the result handler function to handle the speech processing. What do I do if I want to have the result handler AND define a delegate? Must I have my cake but be forced to stare blankly at it rather than eating it?! – Bebhead Jan 19 '17 at 15:53
  • 1
    The delegate provides the method `optional func speechRecognitionTask(_ task: SFSpeechRecognitionTask, didFinishRecognition recognitionResult: SFSpeechRecognitionResult)` which does I believe the same thing as the handler. – TheAmateurProgrammer Jan 19 '17 at 15:59
  • Ah ha! Thank you very much sir. – Bebhead Jan 19 '17 at 16:02
2

I did it like this in ViewController:

recognitionTask = speechRecognizer?.recognitionTask(with: speechRecognitionRequest, delegate: self)
Vitaly Potlov
  • 365
  • 1
  • 7
  • 14