運用runtime可以把一個類方法轉(zhuǎn)成block
importFoundation
importUIKit
classDemo:NSObject{
functest(msg:String){
print(msg)
}
}
let sel =#selector(Demo.test(msg:))
///獲取test(msg:)的方法
let method:Method=class_getInstanceMethod(Demo.self,sel)
///獲取方法實現(xiàn)體的指針
let oldIMP:IMP=method_getImplementation(method)
//把方法體指針轉(zhuǎn)成具體的block
typealiasOldBlockType =@convention(c) (Demo,Selector,String)->Void
let oldBlock =unsafeBitCast(oldIMP, to:OldBlockType.self)
//調(diào)用
oldBlock(Demo(),sel,"調(diào)用方法袁滥。")