In the last blog we introduced the first phase of the messaging mechanism, the super detailed Messaging phase of the Runtime, and now we continue to explore the second and third phases of the messaging mechanism.

A review of the sending phase of the message mechanism

The previous picture was clearly marked, so I will describe it in words:

1. First, it checks whether the incoming receiver is empty. If it is empty, it quits

If no method is found, the call ends. If no method is found, the call ends. If no method is found, the call ends and the method is cached to the receiverClass The cache; The receiverClass cache is used to cache the receiverClass method. If the receiverClass cache is not used, the superClass class_rw_t method list is used to search for the same method ReceiverClass cache, if not found, will continue to find the parent class. If they still can’t find it, they go on to the second stage;

The second phase of the message mechanism: dynamic method resolution

First look at the source code (source search, the last blog has said very clearly, directly follow the last blog said)

Continue to look for the source implementation of resolveMethod_locked, as shown below

Figure 5

Okay, let’s verify that an object method calls the resolveInstanceMethod method in code.

Look at the code below

Let’s go straight to the implementation

BOOL resolveClassMethod:(SEL) resolveClassMethod:(SEL) resolveClassMethod:(SEL) resolveClassMethod:(SEL

From the source code analysis above, the dynamic parsing phase will only be once, it is written and executed only once, when the dynamic method parsing is completed after the second phase is complete

The third stage of the messaging mechanism: message forwarding

As we know from Figure 5 above, after the completion of the second phase, it will return to the previous method and enter one of the message forwarding. Let’s take a look at the source code as shown below

Let’s look at the definition of forward_IMP

Next, let’s look at the definition of _objc_msgForward_impcache, and you’ll find that you’ve found assembly again

Remember to look for arm64, and then look for __objc_forward_handler in the current file. If you can’t find it, search globally (note that one is _ and one is __, as explained above).

You can only see print, you can’t see anything else, but it turns out that the code for forwarding the message is closed source, and we can’t find the implementation, but you can actually look up some pseudo-code on the Internet and see how it works, but that’s okay, so let’s just jump to the conclusion, okay

GDCat,GDCat,GDCat,GDCat,GDCat,GDCat,GDCat

Return [[GDCat alloc]init]; return [[GDCat alloc]init]; return [[GDCat alloc]init]; Objc_msgsend ([[GDCat alloc]init],@selector(test))

If – (id) forwardingTargetForSelector aSelector: (SEL) you don’t have to return to the corresponding, this time we still have the last time of remedial measures, and is also in the implementation, please see the following code

If [NSMethodSignature signatureWithObjCTypes:”v@:”]; Not familiar with

So to write: return the [[[GDCat alloc] init] methodSignatureForSelector: aSelector]; The effect is the same, you can try

There is an interlude here, look at the following code, if the signature returns null

That returns an error, just before the correct version is: doesNotRecognizeSelector: will this error, did not report this error version now, because some interview questions asked, the runtime message mechanism, when will offer doesNotRecognizeSelector it A mistake. This is just pay attention. Let’s move on

Next, we summarize the third stage:

Note:

Careful may find that here I didn’t write the + or – method, because there is a note, is for the message mechanism, many online version is, message mechanism is no third stage for a class method, this is wrong, because you can write directly without prompt, so there is no the third stage, a lot of people think is some, please see the following code

Here’s another one:

So you can see that class methods also have a third-stage messaging mechanism; Here we can use the forwardInvocation to do whatever you want, change the method, change the return value, etc. Here I will not write, interested can discuss 😄

The runtime interview questions

1. Talk about the messaging mechanism of OC

Method calls in OC are actually converted into calls to objc_msgSend, which sends a message to the receiver(selector method name)

Objc_msgSend has three underlying phases…. I’m not going to say that. It’s very clear

2. Talk about the message forwarding mechanism

The third stage, which we mentioned above, is very carefully written,

And so on, with the three stages that we talked about, there are a lot of problems, and so on

Conclusion:

The three phases of the message mechanism are as many as the above, some of them are divided into two phases or four phases, all of which are the same, we just answer the three phases we understand!

I will continue to learn more about Runtime in the next blog post by covering other aspects of runtime.

If you find my writing helpful, please follow me and I will continue to update 😄