[英]Call method with function as argument of Cocoa Touch Framework (swift) from Single View Application (objective-c)
I created Cocoa Touch Framework
in swift
(called MyFramework). 我swift
创建了Cocoa Touch Framework
(称为MyFramework)。 In MyFramework I have method which requires functions as arguments. 在MyFramework中,我有需要函数作为参数的方法。 In short hand MyFramework contains this code: 简而言之, MyFramework包含以下代码:
public class MyFramework : NSObject {
public func MyMethod(successCallback : (String) -> Void, errorCallback : (String, String) -> Void) -> Void {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// successCallback("some value");
// errorCallback("error reason", "error message");
});
}
}
I can include MyFramework
in Single View Application
called MyAppSwift
(in swift
) and successfully call MyMethod of MyFramework in ViewController.swift
by this code: 我可以在名为MyAppSwift
Single View Application
包含MyFramework
(在swift
),并通过以下代码在ViewController.swift
成功调用MyFramework的MyMethod :
class ViewController: UIViewController {
let myframework : MyFramework = MyFramework();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myframework.MyMethod(self.onSuccess, errorCallback : self.onError);
}
private func onSuccess(value : String) {
// ...
}
private func onError(reason : String, error : String) {
// ...
}
}
I can include MyFramework
in Single View Application
called MyAppObjectiveC
(in objective-c) but I do not know how to call MyMethod
of MyFramework in ViewController.m
... 我可以在名为MyAppObjectiveC
Single View Application
包含MyFramework
(在Objective-C中),但我不知道如何在ViewController.m
调用MyFramework的MyMethod
...
// In ViewController.h
#import <UIKit/UIKit.h>
#import <MyFramework/MyFramework.h>
@interface ViewController : UIViewController {
}
@property (strong, nonatomic) MyFramework * myframework;
- (void) onSuccess : (NSString *) value;
- (void) onError : (NSString *) reason : (NSString *) error;
@end
. 。
// In ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myframework = [[MyFramework alloc] init];
[self.myframework MyMethod : self onSuccess, self onError];
}
- (void) onSuccess : (NSString *) value {
// ...
}
- (void) onError : (NSString *) reason : (NSString *) error {
// ...
}
@end
This line [self.myframework MyMethod : self onSuccess, self onError]; 这行[self.myframework MyMethod:self onSuccess,self onError]; is not working. 不管用。 Xcode says expecting identifier ":" after onSuccess and then expects expresion. Xcode在onSuccess之后说期望标识符“:”,然后期望表达式。 Im not sure what to insert. 我不确定要插入什么。 I read several answers on stackoverflow how to pass method as argument in objective-c but no success. 我在stackoverflow上阅读了几个答案,如何在Objective-C中将方法作为参数传递,但没有成功。 For example I have tried selectors [self.myframework MyMethod : @selector(onSuccess:), @selector(onError::)]; 例如,我尝试了选择器[self.myframework MyMethod:@selector(onSuccess :),@ selector (onError::)]]; but Xcode says: "Sending 'SEL' to parameter of incompatible type 'void(^_Nonnull)(NSString * _Nonnull __strong)' 但Xcode说:“将'SEL'发送到不兼容类型'void(^ _ Nonnull)(NSString * _Nonnull __strong)的参数中'
As you pointed out, according to the *-Swift.h
header Swift's MyMethod
is imported into Objective-C as taking blocks as arguments. 如您所指出,根据*-Swift.h
标头,Swift的MyMethod
作为块作为参数导入到Objective-C中。 So, you need to pass it blocks that wrap the Objective-C method calls, something like this: 因此,您需要传递包装了Objective-C方法调用的块,如下所示:
[self.myframework MyMethod: ^ (NSString * s){ [self onSuccess:s]; }
errorCallback: ^(NSString * s1, NSString * s2) { [self onError:s1:s2]; }];
See here for more info about Objective-C blocks: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html 请参阅此处以获取有关Objective-C块的更多信息: https : //developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.