混合開發(fā)項(xiàng)目中蜈敢,Swift頁面跳轉(zhuǎn)到Objective-C頁面時(shí),使用路由方式跳轉(zhuǎn)
當(dāng)傳參為閉包時(shí)候汽抚,swift的閉包不會(huì)被OC識(shí)別抓狭,需要對(duì)閉包轉(zhuǎn)化成能被OC識(shí)別的Block。
Swift代碼
//閉包
let block : (_ isShow: Bool) -> Void = {[weak self]_ in
print("+++=+++++++++++")
}
//把閉包轉(zhuǎn)成可被OC識(shí)別的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;
//調(diào)用Block
if(compatibilityBlockObject){
compatibilityBlockObject(false);
}