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

标签 macos cocoa swift speech-recognition

我对 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/

相关文章:

xcode - 当 .app 在桌面上运行时,NSFileManager 的 currentDirectoryPath 返回根目录

ios - UI View 部分滚动 (iOS)

python - 在 Mac 上安装 Beautifulsoup4 时收到错误消息

swift - NSCollectionView - 自定义 View 项未连接或不会选择

python - 更改 Mac OS 下除终端以外的应用程序的 Python 路径

objective-c - 在单个文档中存储多个文件

ios - 带有准确答案字段的琐事(快速)

swift - 当搜索数据加载到 tableView 中时如何停止 Firestore 分页

cocoa - 系统偏好设置 > 帐户的 uid/名称列表

c++ - 在 MAC OS X 中从 C++ dylib 调用 cocoa/obj-c 方法