gpt4 book ai didi

macos - Swift:无法在 NSSpeechRecognizer 中设置命令

转载 作者:行者123 更新时间:2023-11-28 05:32:09 27 4
gpt4 key购买 nike

我对 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()
}

最佳答案

使用:

SR.commands = commands

您将点符号与旧的 Obj-C 风格的 setter 混合在一起。

你也可以使用 let 作为你的语音识别器,你不需要声明任何一个的类型; Swift 会推断它:

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

关于macos - Swift:无法在 NSSpeechRecognizer 中设置命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27513883/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com