亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么會崩潰?

為什么會崩潰?

MYYA 2019-05-24 10:08:53
代碼如下:+(void)load{staticdispatch_once_tonceToken;dispatch_once(&onceToken,^{Classclass=[NSURLConnectionclass];SELselector=@selector(initWithRequest:delegate:);SELswizzledSelector=@selector(test_initWithRequest:delegate:);MethodoriginalinitWithRequest=class_getInstanceMethod(class,selector);NSURLConnection*(^swizzleBlock)(NSURLConnection*,SEL,NSURLRequest*,id)=^(NSURLConnection*slf,SELswizzledSelector,NSURLRequest*request,iddelegate){return((NSURLConnection*(*)(NSURLConnection*,SEL,NSURLRequest*,id))objc_msgSend)(slf,swizzledSelector,request,delegate);};IMPimplementation=imp_implementationWithBlock(swizzleBlock);class_addMethod(class,swizzledSelector,implementation,method_getTypeEncoding(originalinitWithRequest));MethodnewConnectionWithRequestClassMethod=class_getInstanceMethod(class,swizzledSelector);method_exchangeImplementations(originalinitWithRequest,newConnectionWithRequestClassMethod);});}我看了半天沒發現什么問題,為什么這樣一交換之后運行就會崩潰。在return時候打斷點,信息如下:(lldb)poslf{request:(null)}(lldb)poswizzledSelector{URL:http://httpstat.us/200}(lldb)porequest(lldb)podelegatenil崩潰信息如下:2017-12-1915:09:25.923HookNetwork2[4030:501650]***NSForwarding:warning:selector(0x60000000e020)formessage'X?ü'doesnotmatchselectorknowntoObjectiveCruntime(0x608000004340)--abort2017-12-1915:09:25.924HookNetwork2[4030:501650](null):unrecognizedselectorsenttoinstance0x60000000dea02017-12-1915:09:25.928HookNetwork2[4030:501650]***Terminatingappduetouncaughtexception'NSInvalidArgumentException',reason:'(null):unrecognizedselectorsenttoinstance0x60000000dea0'***Firstthrowcallstack:(0CoreFoundation0x000000010926fb0b__exceptionPreprocess+1711libobjc.A.dylib0x0000000108cd4141objc_exception_throw+482CoreFoundation0x00000001092df134-[NSObject(NSObject)doesNotRecognizeSelector:]+1323CoreFoundation0x00000001091f6840___forwarding___+10244CoreFoundation0x00000001091f63b8_CF_forwarding_prep_0+1205HookNetwork20x00000001086fc379__20+[RAObservererload]_block_invoke_2+1536HookNetwork20x00000001086fd6a3-[ViewControllerconnectTest:]+17317UIKit0x0000000109694d82-[UIApplicationsendAction:to:from:forEvent:]+838UIKit0x00000001098195ac-[UIControlsendAction:to:forEvent:]+679UIKit0x00000001098198c7-[UIControl_sendActionsForEvents:withEvent:]+45010UIKit0x0000000109818802-[UIControltouchesEnded:withEvent:]+61811UIKit0x00000001097027ea-[UIWindow_sendTouchesForEvent:]+270712UIKit0x0000000109703f00-[UIWindowsendEvent:]+411413UIKit0x00000001096b0a84-[UIApplicationsendEvent:]+35214UIKit0x0000000109e945d4__dispatchPreprocessedEventFromEventQueue+292615UIKit0x0000000109e8c532__handleEventQueue+112216CoreFoundation0x0000000109215c01__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__+1717CoreFoundation0x00000001091fb0cf__CFRunLoopDoSources0+52718CoreFoundation0x00000001091fa5ff__CFRunLoopRun+91119CoreFoundation0x00000001091fa016CFRunLoopRunSpecific+40620GraphicsServices0x000000010d044a24GSEventRunModal+6221UIKit0x0000000109693134UIApplicationMain+15922HookNetwork20x00000001086ff2bfmain+11123libdyld.dylib0x000000010c0d965dstart+124???0x00000000000000010x0+1)libc++abi.dylib:terminatingwithuncaughtexceptionoftypeNSException前面的代碼哪里有問題,為什么斷點信息是這樣的?這里有一個crashdemo,可以下載下來看一看。
查看完整描述

2 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

建議閱讀一下imp_implementationWithBlock的文檔。
Parameters
blockTheblockthatimplementsthismethod.Thesignatureofblockshouldbemethod_return_type^(idself,self,method_args…).Theselectorofthemethodisnotavailabletoblock.blockiscopiedwithBlock_copy().
你的代碼里面的block的類型里面是需要了一個seletor,打斷點的得知,此時swizzleselector的值是一個NSURLRequest,明顯不是我們想要的selector,所以objc_msgSend就失敗了。
查閱文檔可知,block的簽名應該是method_return_type^(idself,self,method_args…).是不需要SEL的,所以將上述的代碼改為:
idswizzleBlock=^(NSURLConnection*slf,NSURLRequest*request,iddelegate){
return((NSURLConnection*(*)(NSURLConnection*,SEL,NSURLRequest*,id))objc_msgSend)(slf,swizzledSelector,request,delegate);
};
運行可以成功,點擊按鈕也OK了。
                            
查看完整回答
反對 回復 2019-05-24
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

那個RAObserverer是什么東西?代碼里沒有,奔潰調用棧里看起來跟上面代碼沒關系。
需要知道這兩句是干了什么,從調用??词荝AObserverer沒有load方法的實現
5HookNetwork20x00000001086fc379__20+[RAObservererload]_block_invoke_2+153
6HookNetwork20x00000001086fd6a3-[ViewControllerconnectTest:]+1731
關鍵:imp_implementationWithBlock這個block的參數里是沒有SEL的
                            
查看完整回答
反對 回復 2019-05-24
  • 2 回答
  • 0 關注
  • 598 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號