代碼如下:+(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,可以下載下來看一看。
為什么會崩潰?
MYYA
2019-05-24 10:08:53
