如何从Swift 3应用程序调用Objective C方法?

时间:2017-01-01 18:49:39

标签: ios objective-c swift mobile wit.ai

我正在尝试使用在Swift 3应用中用Objective C编写的wit-ai SDK。

GitHub仓库中的文档解释了如何使用特定的Objective C方法将文本字符串发送给Wit,但我不确定如何在我的应用程序中使用该方法,并且可以使用一些指导。

  

InterpretString

     

向wit.ai发送NSString以进行解释。与发送语音输入相同,但带有文本。

- (void) interpretString: (NSString *) string customData:(id)customData;

这是我到目前为止所做的:

import UIKit

class ViewController: UIViewController, WitDelegate {
    /**
     * Called when the Wit request is completed.
     * param outcomes a NSDictionary of outcomes returned by the Wit API. Outcomes are ordered by confidence, highest first. Each outcome contains (at least) the following keys:
     *       intent, entities[], confidence, _text. For more information please refer to our online documentation: https://wit.ai/docs/http/20141022#get-intent-via-text-link
     *
     * param messageId the message id returned by the api
     * param customData any data attached when starting the request. See [Wit sharedInstance toggleCaptureVoiceIntent:... (id)customData] and [[Wit sharedInstance] start:... (id)customData];
     * param error Nil if no error occurred during processing
     */
    public func witDidGraspIntent(_ outcomes: [Any]!, messageId: String!, customData: Any!, error e: Error!) {
        if ((e) != nil) {
            print("\(e.localizedDescription)")
            return
        }

        let outcomes : NSArray = outcomes! as NSArray
        let firstOutcome : NSDictionary = outcomes.object(at: 0) as! NSDictionary
        let intent : String = firstOutcome.object(forKey: "intent") as! String
        labelView!.text = intent
        labelView!.sizeToFit()
    }

    var labelView : UILabel?
    var witButton : WITMicButton?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // set the WitDelegate object
        Wit.sharedInstance().delegate = self

        // create the button
        let screen : CGRect = UIScreen.main.bounds
        let w : CGFloat = 100
        let rect : CGRect = CGRect(x: screen.size.width/2 - w/2, y: 60, width: w, height: 100)

        witButton = WITMicButton(frame: rect)
        self.view.addSubview(witButton!)

        // create the label
        labelView = UILabel(frame: CGRect(x: 0, y: 200, width: screen.size.width, height: 50))
        labelView!.textAlignment = .center
        labelView!.text = "intent"
        labelView!.textColor = UIColor.black
        labelView!.sizeToFit()
        self.view.addSubview(labelView!)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

0 个答案:

没有答案