How to convert Objective-C to Swift iOS for Temboo Http Post API

114 views Asked by At

Temboo provides Objective-C code for their Choreos (API requests). I used Temboo instructions and able to get the code working inside viewVontroller.m:

#import "ViewController.h"
#import "TMBUtilities.h"
#import "TMBChoreography.h"
#import "TMBTembooSession.h"

@interface Post : NSObject <TMBChoreographyDelegate>
-(void)runPostChoreo;
-(void)choreographyDidFailWithError:(NSError*)error;
-(void)choreographyDidFinishExecuting:    
(TMBUtilities_HTTP_Post_ResultSet*)result;
@end

@implementation Post
-(void)runPostChoreo {
    TMBTembooSession *session = [[TMBTembooSession alloc] initWithAccount:@"xxxxx" appKeyName:@"xxxxx" andAppKeyValue:@"xxxxx"];
    TMBUtilities_HTTP_Post *postChoreo = [[TMBUtilities_HTTP_Post alloc] initWithSession:session];
    TMBUtilities_HTTP_Post_Inputs *postInputs = [postChoreo newInputSet];
    [postInputs setUsername:@"xxxxx"];
    [postInputs setURL:@"https://anywebsite.com/notes"];
    [postInputs setPassword:@"xxxxx"];
    [postInputs setRequestParameters:@"{\"utf8\":\"xxxxx\",\"xxxxx\":\"Post This Info\"}"];
    [postChoreo executeWithInputs:postInputs delegate:self];
}

-(void)choreographyDidFailWithError:(NSError*)error {
    NSLog(@"Error - %@", error);
}

-(void)choreographyDidFinishExecuting:(TMBUtilities_HTTP_Post_ResultSet*)result {
    NSLog(@"%@", [result getHTTPLog]);
    NSLog(@"%@", [result getResponseStatusCode]);
    NSLog(@"%@", [result getResponse]);
}

@end

@implementation ViewController
    - (IBAction)buttonPost:(id)sender {
         dispatch_async(dispatch_get_main_queue(), ^{
         Post *test = [[Post alloc] init];
          [test runPostChoreo];
    });
}

for Swift conversion, I have following bridging table

#import "TMBUtilities.h"
#import "TMBChoreography.h"
#import "TMBTembooSession.h"

Then I added converted Temboo code at the top of the ViewController.Swift. I am getting following error at the top line "Type 'Post' does not conform to protocol 'TMBChoreographyDelegate'". I will appreciate any hint on this. Thanks.

class Post: NSObject, TMBChoreographyDelegate {
    func runPostChoreo() {}
    func choreographyDidFailWithError(error:NSError) {}
    func choreographyDidFinishExecuting(result: TMBUtilities_HTTP_Post_ResultSet) {}
}

class Post{

    func runPostChoreo() {
        let session: TMBTembooSession = TMBTembooSession(account: "xxxxx", appKeyName: "xxxxx", andAppKeyValue: "xxxxx")
        let postChoreo: TMBUtilities_HTTP_Post = TMBUtilities_HTTP_Post(session: session)
        let postInputs: TMBUtilities_HTTP_Post_Inputs = postChoreo.newInputSet()
        postInputs.setUsername("xxxxx")
        postInputs.setURL("https://anywebsite.com/notes")
        postInputs.setPassword("xxxxx")
        postInputs.setRequestParameters("{\"utf8\":\"xxxx\",\"xxxxx\":\"Post This Info\"}")
        postChoreo.executeWithInputs(postInputs, delegate: self)
    }

    func choreographyDidFailWithError(error: NSError) {
        NSLog("Error - %@", error)
    }

    func choreographyDidFinishExecuting(result: TMBUtilities_HTTP_Post_ResultSet) {
         NSLog("%@", result.getHTTPLog())
         NSLog("%@", result.getResponseStatusCode())
         NSLog("%@", result.getResponse())
    }
}

class ViewController: UIViewController {
    @IBAction func myButton(sender: AnyObject) {
    dispatch_async(dispatch_get_main_queue(), {() -> Void in
        var test: Post = Post()
        test.runPostChoreo()
        myRunLoop.run()
    })
1

There are 1 answers

1
ryantxr On

I'm not sure I understand why you declared class Post 2 times in your Swift code. I assume it's because the Objective-C code has an interface and an implementation. Swift does not do things that way.

Change the code to only have class Post once as follows:

TMBChoreographyDelegate already inherits NSObject so there is no need to do it again.

@protocol TMBChoreographyDelegate <NSObject>

Proposed code rewrite:

class Post: TMBChoreographyDelegate{

    // This function is required for TMBChoreographyDelegate
    func runPostChoreo() {
        let session: TMBTembooSession = TMBTembooSession(account: "xxxxx", appKeyName: "xxxxx", andAppKeyValue: "xxxxx")
        let postChoreo: TMBUtilities_HTTP_Post = TMBUtilities_HTTP_Post(session: session)
        let postInputs: TMBUtilities_HTTP_Post_Inputs = postChoreo.newInputSet()
        postInputs.setUsername("xxxxx")
        postInputs.setURL("https://anywebsite.com/notes")
        postInputs.setPassword("xxxxx")
        postInputs.setRequestParameters("{\"utf8\":\"xxxx\",\"xxxxx\":\"Post This Info\"}")
        postChoreo.executeWithInputs(postInputs, delegate: self)
    }

    // This function is required for TMBChoreographyDelegate
    func choreographyDidFailWithError(error: NSError) {
        print("Error - \(error)")
    }

    func choreographyDidFinishExecuting(result: TMBUtilities_HTTP_Post_ResultSet) {
        print(result.getHTTPLog())
        print(result.getResponseStatusCode())
        print(result.getResponse())
    }
}