0

Temboo 为他们的 Choreos(API 请求)提供了 Objective-C 代码。我使用了 Temboo指令并能够让代码在 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];
    });
}

对于 Swift 转换,我有以下桥接表

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

然后我在 ViewController.Swift 的顶部添加了转换后的 Temboo 代码。我在第一行出现以下错误"Type 'Post' does not conform to protocol 'TMBChoreographyDelegate'"。我将不胜感激任何提示。谢谢。

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()
    })
4

1 回答 1

0

我不确定我是否理解你为什么在你的 Swift 代码中声明类 Post 2 次。我认为这是因为 Objective-C 代码有一个接口和一个实现。Swift 不会那样做。

将代码更改为只有一次 Post 类,如下所示:

TMBChoreographyDelegate 已经继承了 NSObject 所以没有必要再做一次。

@protocol TMBChoreographyDelegate <NSObject>

建议的代码重写:

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())
    }
}
于 2016-03-13T23:05:44.960 回答