If you learn more about iOS Runtime, you have to mention message forwarding, and many implementations of the framework are based on this feature (e.g. Jspatch)
Although read a lot of articles about message forwarding, but not very thorough understanding, or the practice of some of their own understanding can be more thorough.
First I define a mystring inheritance NSString
@interface mystring:nsstring @end @implementation MyString @end
Then create a mystring, call missmethod,missmethod1,missmethod2, and so on by Performselector.
-(void) testforward{ *str = [[MyString alloc] init]; [Str performselector: @selector (Missmethod) Withobject:nil]; [Str performselector: @selector (MISSMETHOD2) Withobject:nil]; [Str performselector: @selector (MISSMETHOD3) withobject:nil];}
If you do not write anything, this will certainly crash, this error [NSObject (NSObject) Doesnotrecognizeselector:], because mystring does not have these three methods, and the parent class does not.
If the method is not found, the system will try to remedy, to see if there is no ability to handle, first call Resolveinstancemethod this method, the default method is to return no, walk a step process, if you return yes, such as the implementation of the following method, If the name of the incoming SEL is the beginning of missmethod, it is considered that our class can handle this method. And if it's the Missmethod method, we'll add a method dynamicmethodimp to the class as the Missmethod implementation. At this point, when the outside call Missmethod, it is actually called Dynamicmethodimp
void dynamicmethodimp (ID self , SEL _cmd) {
NSLog(@ ">> dynamicmethodimp");
}
+(BOOL) Resolveinstancemethod: (SEL) name{NSLog (@">> Instance Resolving%@", Nsstringfromselector (name)); NSString*selname =nsstringfromselector (name); if([Selname Hasprefix:@"Missmethod"]) { if(Name = =@selector (Missmethod)) {Class_addmethod ([ Selfclass], name, (IMP) Dynamicmethodimp,"[email protected]:"); returnYES; } Else { returnNO; } } return[Super Resolveinstancemethod:name];}
If the Resolveinstancemethod method returns no, what happens next? First I defined a class MyString2 as the Tray-man, realizing the Missmethod1,missmethod2 method
@interface mystring2:nsstring @end @implementation MyString2 -(void ) missmethod2{NSLog ( @ " missmethod2 " -(void ) missmethod3{NSLog ( @" missmethod3 "" Span style= "color: #000000");} @end
When the Resolveinstancemethod method returns no, the system tries to call the following method to see if there is a tray to pick up the pot, _STR2 is an example of MyString2 and implements the MissMethod2 method. When the mystring example calls the MissMethod2 method, Missmethod2->resolveinstancemethod (NO)->forwardingtargetforselector, Return a tray to send the message to this example objc_sendmsg (_str2,sel)
-(ID) forwardingtargetforselector: (SEL) sel { if(sel = = @selector (MissMethod2)) { return _str2; } return [Super Forwardingtargetforselector:sel];}
When Forwardingtargetforselector is unable to process, return nil, the following will go to this method Methodsignatureforselector, to determine whether the sig is nil, If you do not go forwardinvocation for nil, and finally call Forwardinvocation, if this method is not processed, the last attempt will throw the exception doesnotrecognizeselector
-(Nsmethodsignature *) Methodsignatureforselector: (SEL) sel {nsmethodsignature*Sig; Sig=[_str2 Methodsignatureforselector:sel]; if(SIG) {returnSig; } return[Super Methodsignatureforselector:sel];}- (void) Forwardinvocation: (Nsinvocation *) Invocation {IDtarget =Nil; if([_str2 methodsignatureforselector:[invocation selector]]) {target=_str2; [Invocation invokewithtarget:target]; }}
Summarize the whole process of message forwarding, approximate process missmethod->resolveinstancemethod (NO)->forwardingtargetforselector (nil) Methodsignatureforselector (SIG)->forwardinvocation.
The following is the complete code implementation:
////MYSTRING.M//OBJC////Created by Zilong.li on 2017/8/17.////#import "MyString.h"#import<objc/runtime.h>@interfacemystring2:nsstring@end@implementationMyString2- (void) missmethod2{NSLog (@"MissMethod2");}- (void) missmethod3{NSLog (@"MissMethod3");}@endvoidDynamicmethodimp (IDSelf , SEL _cmd) {NSLog (@">> Dynamicmethodimp");}@interfaceMyString () {MyString2*_str2;}@end@implementationMyString-(instancetype) init{ Self=[Super Init]; if(self) {_str2=[[MyString2 alloc] init]; } returnSelf ;}+(BOOL) Resolveinstancemethod: (SEL) name{NSLog (@">> Instance Resolving%@", Nsstringfromselector (name)); NSString*selname =nsstringfromselector (name); if([Selname Hasprefix:@"Missmethod"]) { if(Name = =@selector (Missmethod)) {Class_addmethod ([ Selfclass], name, (IMP) Dynamicmethodimp,"[email protected]:"); returnYES; } Else { returnNO; } } return[Super Resolveinstancemethod:name];}- (ID) Forwardingtargetforselector: (SEL) sel {if(sel = =@selector (MISSMETHOD2)) { return_str2; } return[Super Forwardingtargetforselector:sel];}-(Nsmethodsignature *) Methodsignatureforselector: (SEL) sel {nsmethodsignature*Sig; Sig=[_str2 Methodsignatureforselector:sel]; if(SIG) {returnSig; } return[Super Methodsignatureforselector:sel];}- (void) Forwardinvocation: (Nsinvocation *) Invocation {IDtarget =Nil; if([_str2 methodsignatureforselector:[invocation selector]]) {target=_str2; [Invocation invokewithtarget:target]; }}@end
iOS Message Forwarding Learning notes