Recently, the information forwarded by the news, found that most of the theoretical knowledge, very few complete code. Now in the form of code to explain the image:
Create a project with Xcode
1. Normal method invocation
Create a class person code as follows
The Person.h code is as follows:
#import <Foundation/Foundation.h>@interface person:nsobject- (Instancetype) Init ns_unavailable; -(Instancetype) Initwithusername: (NSString *) userName; -(void) logusername; @end
The PERSON.M code is as follows:
#import "Person.h"@interfacePerson () @property (nonatomic, copy) NSString*UserName;@end@implementation Person-(Instancetype) Initwithusername: (NSString *) username{ Self=[Super Init]; if(self) {_username=[userName copy]; } returnSelf ;}- (void) logusername{NSLog (@"userName =%@", Self.username);}@end
The VIEWCONTROLLER.M code is as follows:
#import "ViewController.h"#import "Person.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Person *person = [[Person alloc] Initwithusername:@"Xiao Wang"]; [Person Logusername];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
The result of running the project is:
$-07.296 runtimedemo[24364 :1776826] userName = Xiao Wang
Results are normal.
2. Unrecognized selector situation
Remove the person.m Logusername method, at this point the code for PERSON.M is as follows:
#import " Person.h " @interface *userName; @end @implementation Person-(Instancetype) Initwithusername: (NSString *) username{ = [Super init]; if (self) { = [userName copy]; } return Self ;} @end
The following results can be found in running the project
.-Geneva- on at: .:25.788runtimedemo[24729:1788071]-[person Logusername]: Unrecognized selector sent to instance0x608000018e00 .-Geneva- on at: .:25.796runtimedemo[24729:1788071] * * * * terminating app due to uncaught exception'nsinvalidargumentexception', Reason:'-[person Logusername]: Unrecognized selector sent to instance 0x608000018e00'
If you do not implement the Logusrname method in the person class, you can also allow the project to run normally, when it comes to message forwarding, the OBJECTIVE-C runtime will give three times the chance to save the program crashes.
As follows:
(1). Method resolution
The OBJC runtime calls +resolveinstancemethod: or +resolveclassmethod:, giving you a chance to provide a function implementation. If you add a function, the runtime will restart the message sending process, otherwise the runtime will move to the next step, message forwarding (msg Forwarding).
(2). Fast forwarding
If the target object implements-forwardingtargetforselector:,runtime then this method is called, giving you the opportunity to forward the message to other objects. As long as this method returns not nil and self, the entire message sending process is restarted and of course the sent object becomes the object you return. Otherwise, you will continue to normal fowarding. This is called fast, just to differentiate the next forwarding mechanism. Because this step does not create any new objects, the next forwarding creates a Nsinvocation object, so it is relatively faster.
(3). Normal forwarding
This step is the last time the runtime gives you a chance to save. First it sends-methodsignatureforselector: The message gets the parameters of the function and the return value type. If-methodsignatureforselector: Return nil,runtime will issue-doesnotrecognizeselector: message, the program is also hanging off. If a function signature is returned, runtime creates a Nsinvocation object and sends a-forwardinvocation: message to the target object.
The following verifies the execution order of the corresponding function:
The code for PERSON.M is as follows:
#import "Person.h"#defineLog_functionname NSLog (@ "%s", __func__)@interfacePerson () @property (nonatomic, copy) NSString*UserName;@end@implementation Person-(Instancetype) Initwithusername: (NSString *) username{ Self=[Super Init]; if(self) {_username=[userName copy]; } returnSelf ;}+(BOOL) Resolveinstancemethod: (SEL) sel{log_functionname; returnYES;}- (ID) Forwardingtargetforselector: (SEL) aselector{log_functionname; return[Super Forwardingtargetforselector:aselector];}-(Nsmethodsignature *) Methodsignatureforselector: (SEL) aselector{log_functionname; return[Super Methodsignatureforselector:aselector];}- (void) Forwardinvocation: (Nsinvocation *) aninvocation{Log_functionname;}@end
The result of the operation is:
.-Geneva- on at: $:21.723runtimedemo[25672:1817388] +[person Resolveinstancemethod:] .-Geneva- on at: $:21.724runtimedemo[25672:1817388] -[person Forwardingtargetforselector:] .-Geneva- on at: $:21.724runtimedemo[25672:1817388] -[person Methodsignatureforselector:] .-Geneva- on at: $:21.724runtimedemo[25672:1817388] +[person Resolveinstancemethod:] .-Geneva- on at: $:21.725runtimedemo[25672:1817388]-[person Logusername]: Unrecognized selector sent to instance0X60000001CEF0 .-Geneva- on at: $:21.729runtimedemo[25672:1817388] * * * * terminating app due to uncaught exception'nsinvalidargumentexception', Reason:'-[person Logusername]: Unrecognized selector sent to instance 0X60000001CEF0'
The results of the operation verify the above instructions.
IOS message Forwarding