简体   繁体   English

从Single View Application(objective-c)以函数作为Cocoa Touch Framework(swift)的参数的调用方法

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM