混合开发时Swift路由跳转传参闭包给Objective-C

混合开发项目中,Swift页面跳转到Objective-C页面时,使用路由方式跳转
当传参为闭包时候,swift的闭包不会被OC识别,需要对闭包转化成能被OC识别的Block。
Swift代码

//闭包
let block : (_ isShow: Bool) -> Void = {[weak self]_ in
            print("+++=+++++++++++")
  }
//把闭包转成可被OC识别的Block
  let compatibilityBlock = block as @convention(block) (Bool) -> Void
  let compatibilityBlockObject = unsafeBitCast(compatibilityBlock, to: AnyObject.self)
//路由
Router.share()?.run(withURL: "XXXXXXX", params: ["block":compatibilityBlockObject])

Objective-C代码


//定义block
typedef void(^agreeFinishBlock)(Bool isShow);

//Block赋值
agreeFinishBlock block =  compatibilityBlockObject;

//调用Block
if(compatibilityBlockObject){
     compatibilityBlockObject(false);
}

你可能感兴趣的:(混合开发时Swift路由跳转传参闭包给Objective-C)