Swift:无法在NSSpeechRecognizer中设置命令

时间:2014-12-16 21:05:22

标签: macos cocoa swift speech-recognition

我遇到setCommands问题。我无法在项目中为setCommands设置命令。在viewDidLoad中,我尝试了SR.setCommands =命令,但是我收到一条错误说"' NSSpeechRecognizer'没有名为' setCommands'"对此有何帮助?

class ViewController: NSViewController, NSSpeechRecognizerDelegate {

@IBOutlet weak var Label: NSTextField!

var SR:NSSpeechRecognizer = NSSpeechRecognizer()
var commands: [AnyObject]? = ["word","sad","happy"]

override func viewDidLoad() {
    super.viewDidLoad()

    SR.setCommands = commands //Error
}

override var representedObject: AnyObject? {
    didSet {
    // Update the view, if already loaded.
    }
}

@IBAction func Listen(sender: AnyObject) {
    SR.startListening(); print("listening")
}

func speechRecognizer(sender: NSSpeechRecognizer,
    didRecognizeCommand command: AnyObject?){

        if (command as String == "word")
        {
            println("Hello")
        }
        else
        {
            println("NotWord")
        }

    }

@IBAction func Stop(sender: AnyObject) {
    SR.stopListening()
}

}

1 个答案:

答案 0 :(得分:2)

使用:

SR.commands = commands

你将点符号与旧的Obj-C风格的二传手混合在一起。

您还可以将let用于您的语音识别器,并且您不需要声明任何一种类型;斯威夫特会推断它:

let SR = NSSpeechRecognizer()
var commands = ["word","sad","happy"]