Question: How can I call an Objective C method from a Swift 3 app?

Question

How can I call an Objective C method from a Swift 3 app?

Answers 0
Added at 2017-01-01 19:01
Tags
Question

I'm trying to use wit-ai SDK, written in Objective C in a Swift 3 app.

The documentation in the GitHub repo explains how to use a particular Objective C method to send text string to Wit, but I'm not sure how to use that method in my app, and could use some guidance.

InterpretString

Sends an NSString to wit.ai for interpretation. Same as sending a voice input, but with text.

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

This is what I have so far:

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.
    }
}
Answers to

How can I call an Objective C method from a Swift 3 app?

Source Show
◀ Wstecz