The delegate function is never called. Do you know what's going on?

import Cocoa
import Foundation
import AppKit

class ViewController: NSViewController, NSSpeechRecognizerDelegate {

    var speechRec: NSSpeechRecognizer = NSSpeechRecognizer()!
    var commands = ["a","b","c","d","stop","go"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        speechRec.commands = commands
        speechRec.delegate = self
        //speechRec.listensInForegroundOnly = true
        //speechRec.blocksOtherRecognizers = true
        speechRec.startListening()
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    @IBAction func Listen(_ sender: Any) {
        speechRec.startListening()
        print("Listening now...")
    }
    
    @IBAction func Stop(_ sender: Any) {
        speechRec.stopListening()
        print("Stopped Listening...")
    }

    func speechRecognizer(_ sender: NSSpeechRecognizer, didRecognizeCommand command: String) {

        print("Recognized...")
        
        if (command as String == "a") {
            print("You picked \(command)")              // Debug print
        }
        else if (command as String == "b") {
            print("You picked \(command)")              // Debug print
        }
        else if (command as String == "c") {
            print("You picked \(command)")              // Debug print
        }
        else if (command as String == "d") {
            print("You picked \(command)")              // Debug print
        }
        else if (command as String == "stop") {
            print("You picked \(command)")              // Debug print
        }
        else if (command as String == "go") {
            print("You picked \(command)")              // Debug print
        }
        else {
            print("You picked \(command)")
        }
    }
}


Any ideas? Basically, the code is supposed to print a message on recognition of the stop words in the commands array. Nothing is being recognized. What should I do?

Replies

Have you completed plist ?


provide a usage description displayed when permission is requested. Open Info.plist and add the key

Privacy - Speech Recognition Usage Description
providing the String value
I want to write down everything you say
:

I set the pllst file for Microphone Usage and Speech Recognition Usage but it didn't help. Any other ideas?


>I set the pllst file for...but it didn't help


Always follow up such changes with an option-clean build folder via Xcode's Product menu.