gpt4 book ai didi

ios - 如何将 Temboo Http Post API 的 Objective-C 转换为 Swift iOS

转载 作者:行者123 更新时间:2023-11-30 13:36:48 25 4
gpt4 key购买 nike

Temboo 为其 Choreos(API 请求)提供 Objective-C 代码。我用过Temboo instructions并能够让代码在 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 代码。我在顶行收到以下错误“类型‘Post’不符合协议(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()
})

最佳答案

我不确定我是否理解为什么您在 Swift 代码中声明了类 Post 2 次。我认为这是因为 Objective-C 代码有一个接口(interface)和一个实现。 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())
}
}

关于ios - 如何将 Temboo Http Post API 的 Objective-C 转换为 Swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com