如何为Temboo Http Post API将Objective-C转换为Swift iOS
Temboo为他们的Choreos(API请求)提供Objective-C代码。我使用了Temboo,并且能够在ViewVoController中运行代码。m:如何为Temboo Http Post API将Objective-C转换为Swift iOS,ios,objective-c,swift,temboo,Ios,Objective C,Swift,Temboo,Temboo为他们的Choreos(API请求)提供Objective-C代码。我使用了Temboo,并且能够在ViewVoController中运行代码。m: #import "ViewController.h" #import "TMBUtilities.h" #import "TMBChoreography.h" #import "TMBTembooSession.h" @interface Post : NSObject <TMBChoreographyDelegate>
#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];
});
}
然后我在ViewController.Swift的顶部添加了转换后的Temboo代码。我在顶行收到以下错误:“键入'Post'不符合协议'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代码中声明了2次类Post。我认为这是因为Objective-C代码有一个接口和一个实现。斯威夫特不会那样做 将代码更改为只发布一次类帖子,如下所示: TMBChoreographyDelegate已继承NSObject,因此无需再次执行此操作
@protocol TMBChoreographyDelegate <NSObject>
我想知道问题是否与swift如何从方法中删除“WithError”有关(如中所述)。如果您已导入带有桥接头的ObjC代码,为什么要在swift中重新定义整个类?您应该已经能够使用swift访问该类。尝试删除post和access的整个swift实现,就像您通常会做的那样。第二个
类post
不应该是扩展post
?我删除了类ViewController:UIViewController{
上面的所有内容,并使用了3个函数(runPostCheoreo(),…)在类ViewController内部。然后我在委托:self处出错,并转换为委托:nil。它起作用了。postChoreo.executeWithInputs(postInputs,delegate:nil)
谢谢。Blake-你是对的,我不需要优化。谢谢。我删除了类ViewController:UIViewController上面的所有内容{
并在类ViewController中使用了3个函数(runPostCheoreo(),…)。然后我在委托处出错:self并转换为委托:nil。它成功了。postChoreo.executeWithInputs(postInputs,delegate:nil)
。错误是无法将“ViewController”类型的值转换为预期的参数类型“TMBChoreographyDelegate”。
@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())
}
}