将 objective-c 完成处理程序传递给 swift

Pass objective-c completion handler to swift

使用ios pushkit didReceiveIncomingPushWithPayload;调用并传递多个参数,包括 withCompletionHandler。 withCompletionHandler 的函数调用、接口和 swift 参数是什么样的?

AppDelegate.m

- (void)pushRegistry:(PKPushRegistry *)registry
  didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
             forType:(PKPushType)type
  withCompletionHandler:(void (^)(void))completion{
  NSLog(@"VHC: AppDelegate - didReceiveIncomingPushWithPayload hit!");
  Voip* instance = [Voip new];
  [instance handleIncomingPush:payload forType:type withCompletionHandler:completion];
}

Voip.h

-(void)handleIncomingPush: (PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion;

Voip.swift

 @objc(handleIncomingPush: : :)
  func handleIncomingPush(_ payload: PKPushPayload, forType: PKPushType, withCompletionHandler: @escaping () -> Void) {
    withCompletionHandler()
}

错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Voip handleIncomingPush:forType:withCompletionHandler:]: unrecognized selector sent to instance 0x2806f4f30'

一些观察:

  1. 那个@objc方法名不对。鉴于您的 Swift 方法签名与您在 Objective-C 代码中查找的内容相匹配,您可以删除手动方法名称:

    class Voip: NSObject {
        @objc func handleIncomingPush(_ payload: PKPushPayload, forType: PKPushType, withCompletionHandler: @escaping () -> Void) {
            withCompletionHandler()
        }
    }
    

    或者,就我个人而言,如果需要,我会使用 Swift 层方法签名并声明 ObjC 接口:

    class Voip: NSObject {
        @objc(handleIncomingPush:forType:withCompletionHandler:)
        func handleIncomingPush(_ payload: PKPushPayload, type: PKPushType, completionHandler: @escaping () -> Void) {
            completionHandler()
        }
    }
    
  2. 删除Voip.hheader。总是只使用“Objective-C 生成的接口 Header 文件”。

  3. 不要在 Objective-C 代码中导入 Voip.h,而是导入生成的接口 header。例如。如果您的应用名为 MyApp,那么它可能只是:

    #import <MyApp-Swift.h>
    

    如果您不清楚名称是什么,请查看“Objective-C 生成的界面 Header 文件”设置的目标设置。