Article we introduced the method of dynamic resolution, for us to understand the system when the message is sent to find IMP, after the cache lookup and method through the list to find, if found no IMP will enter the resolution method of dynamic process, we also analyzed the if in the process of dynamic method resolution for message interception, the advantages and disadvantages of Also mentioned is a new thing message forwarding, so today we will start to explore the message forwarding process.

forward

Let’s create a new project, create a KGTeacher class, inherit from NSObject, and add a method – (**void**)sayHello; Then we add the following code to main.m:

2, then the instrumentObjcMessageSends do? We can see in the objc source code, the following code:

3. Then above it we can see the following piece of code:

For example, if you want to send a message to a given msgsend, send it to/TMP /msgSends-%d. For example, if you want to send it to a given msgsend, send it to/TMP /msgSends-%d. For example, if you want to send it to/TMP /msgSends-%d, send it to/TMP /

5. After opening the log, we can find that the method called by message sending is recorded in detail, as shown in the figure below:

6. Then we can proceed to the next step, which is the message forwarding process.

Fast message forwarding

  • Object method message forwarding

1, we through the above steps as you can see, the system is a method of dynamic resolution, namely resolveInstanceMethod before he started forward, then the first call is forwardingTargetForSelector, Then let’s search the objc source code to see how the system is implemented:

2, you can see through the source code is a + method is a – method, the corresponding is a class method and object method. And the return value is of type ID, because we are calling the object method, so we should return an object here, so let’s implement it ourselves, give an object, and see if we can achieve fast message forwarding.

(**void**)sayHello ()sayHello ()sayHello ()sayHello ()sayHello ()sayHello ()sayHello ()sayHello ()sayHello

4. Then we carry out rapid message forwarding in the KGTeacher class. The specific code is as follows:

5, then we run the project to see what the specific effect is, whether the message can be quickly forwarded? Here are the results:

  • Class method messages are forwarded quickly

1, when we step above to see, see the forwardingTargetForSelector has a + method, we just use method, so if we can guess the method is used to handle class news fast forward? Next we expose a class method in the KGTeacher class, then do not implement it, and then call it in main.m, as follows:

2. Then we run the code and see the result:

3. We can see that the message is forwarded successfully. Since we saw three more methods after the fast forwarding process, let’s continue to see what the methods do and how to use them.

Slow message forwarding

  • Object method messages are slowly forwarded

1, first of all, we look at the specific implementation of the methodSignatureForSelector this function, through the search in objc source code, we can see the following code:

2. But how? Since it requires an NSMethodSignature, we’re going to return an NSMethodSignature, but how do we write it? Let’s take a look at what methods the system gives me, how this NSMethodSignature should be created, and open it with option+ right click:

Const char* (const char*); Encodings (const char*); Encodings (const char*) So all we need to do is get the Method Type Encodings, which we can do with method_getTypeEncoding, but we need to get a Method, Now that we know SEL we can get it from class_getInstanceMethod, because we’re calling an object method. The specific code is as follows:

4, then we directly run the program, found that the program or crash, or error method can not find the implementation, so where is the problem? When I checked the official document, there was a prompt invocation, and it is also in the print. Is that the problem? Let’s try to implement this because it has an NSInvocation, and then let’s look at the implementation of the class as follows:

5. Then we find the problem, we need a target to implement the method, so we modify the code, the final result is as follows:

6. Then we run the code again and look at the result:

  • Class method messages are forwarded slowly

1, the same we forward messages to the Method of class slow, we also realize methodSignatureForSelector the function, and then use the class_getClassMethod to obtain Method because we class Method for the forward. The specific code is as follows:

2. Then we run the code and see what it looks like:

3. Message forwarding is successful, which means that we have realized the slow forwarding perfectly. How to use it in the development process needs to be combined with the actual business to expand.

DoesNotRecognizeSelector (doesNotRecognizeSelector)

5. Therefore, this method is the final direction of message forwarding. If it is not found after several times of remediation, then enter this method directly and print the prompt message.

conclusion

  • Message forwarding Flowchart