【问题标题】:Swift: unable to set commands in NSSpeechRecognizerSwift:无法在 NSSpeechRecognizer 中设置命令
【发布时间】:2014-12-17 05:09:29
【问题描述】:

我对 setCommands 有疑问。我无法在我的项目中设置 setCommands 的命令。在 viewDidLoad 我尝试了 SR.setCommands = commands 但我收到一条错误消息“'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()
}

}

【问题讨论】:

    标签: macos cocoa swift speech-recognition


    【解决方案1】:

    用途:

    SR.commands = commands
    

    您将点表示法与旧的 Obj-C 样式设置器混合在一起。

    您也可以将let 用于您的语音识别器,您无需说明两者的类型; Swift 会推断它:

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

    【讨论】:

    • 非常感谢?!但是我如何将口语与我的命令进行比较,因为现在我的 if 语句似乎什么都不做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2018-06-10
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多