swift - 向 wit.ai 发送文本时接收结果的问题

标签 swift swift2 nsarray nsdictionary wit.ai

我使用以下方法通过按钮按下功能向 wit.ai 发送文本:

@IBAction func searchButton(sender: AnyObject) {

    searchQueryText = searchTextInput.text!
    if searchQueryText != "" {
        wit.interpretString(searchQueryText, customData: nil)
    }

func interpretString(string: String, customData: AnyObject) {
}

当文本发送到 wit.ai 时,效果很好。然而,我没有收到 wit.ai 对应用程序的回复。如果使用麦克风,我可以获得很好的响应,但不能得到文本。我尝试调用 witDidGraspIntent 函数来强制它在按下按钮时运行,但我无法弄清楚应该在“结果”参数中使用什么。有人可以帮忙吗?我不确定按下按钮后是否有不同的方式来运行该功能?这是函数:

func witDidGraspIntent(outcomes: [AnyObject]!, messageId: String!, customData: AnyObject!, error e: NSError!) {
    if ((e) != nil) {
        print("\(e.localizedDescription)")
        return
    }

    let outcomes : NSArray = outcomes!
    let firstOutcome : NSDictionary = outcomes.objectAtIndex(0) as! NSDictionary
    if let intent = firstOutcome.objectForKey("intent") as? String {
        searchResultsIntent = intent
    }

    if searchResultsIntent == "searchIntent" {

        intentLabel.text = "\(searchResultsIntent)"
        print(outcomes[0])

    } else {

        intentLabel.text = "I'm sorry, I did not understand that."
    }
}

这里是 wit.ai 的文档:https://wit.ai/docs/ios/4.0.0/api

非常感谢任何帮助!

干杯。

最佳答案

Wit sdk 提供了一个共享实例(单例)供用户使用,因此您可以像这样启动它:

Wit.sharedInstance().accessToken = "TOKEN"
Wit.sharedInstance().delegate = self

并使用sharedInstance调用interpretString函数,即

 Wit.sharedInstance().interpretString(text, customData: nil)

关于swift - 向 wit.ai 发送文本时接收结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36399273/

相关文章:

ios - 在 UIPageViewController swift 的最后一页隐藏点

ios - 按字符串属性的首字母过滤数组

iphone - 解析 nsmutabledictionary 将用户电话号码存储在数组中

ios - 如果我调用 cancelAllOpeartions,OperationQueue 不会从队列中删除操作

swift - 从文本文件中读取和写入字符串

ios - 使用 Swift 获取核心数据请求中的内存泄漏

ios - UiTableView 以编程方式按部分组织项目

swift2 - 如何在映射中抛出错误以将字符串转换为整数数组以获取数字格式异常

ios - swift 2.0 : Type of Expression is ambiguous without more context?

cocoa - NSLog NSArray 的地址