0

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()
    })
antbithia
  • 49
  • 1
  • 7
  • I wonder if the problem is related to how swift removes "WithError" from methods (as described in [Adopting Cocoa Design Patterns](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html)). – Aaron Brager Mar 13 '16 at 21:51
  • If you have imported the ObjC code with bridging headers why are you redefining the entire class in swift? You should be able to access the class already with swift. Try removing the entire swift implementation of post and access `Post` as you normally would. – Blake Lockley Mar 13 '16 at 21:54
  • Shouldn't the second `class Post` not be `extension Post`? – SKerkewitz Mar 13 '16 at 23:09
  • I removed all above the `class ViewController: UIViewController { ` and used 3 functions (runPostCheoreo(),...) inside the class ViewController. Then I got error at the delegate: self and converted to delegate: nil. It worked. `postChoreo.executeWithInputs(postInputs, delegate: nil)` Thanks. – antbithia Mar 13 '16 at 23:32
  • Blake - you are correct I did not need to refine. Thanks. – antbithia Mar 13 '16 at 23:34

1 Answers1

0

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())
    }
}
ryantxr
  • 4,119
  • 1
  • 11
  • 25
  • I removed all above the `class ViewController: UIViewController { ` and used 3 functions (runPostCheoreo(),...) inside the class ViewController. Then I got error at the delegate: self and converted to delegate: nil. It worked. `postChoreo.executeWithInputs(postInputs, delegate: nil)`. The error was **Cannot convert value of type 'ViewController' to expected argument type 'TMBChoreographyDelegate'**. – antbithia Mar 13 '16 at 23:31