preface

As an iOS developer, we all know that OC is a dynamic Runtime language, based on the Runtime mechanism at the bottom, so what is Runtime? What role does it play in running our code? In addition, we call a method in our code. How does this method get called under the Runtime mechanism? Let’s explore it today

One: What is Runtime

Run-time is code that runs and gets loaded into memory, and it can’t be called by the system until it’s loaded into memory, it can’t be made alive, it can’t be recognized by the system until it’s stored on disk and not loaded into memory; There have been two Runtime versions over the years. One is Legacy (Objective-C 1.0), which was the early version and ran primarily on Objective-C 1.0, 32-bit Mac OS X platforms. The Modern version (Objective-C 2.0) is the one we’re using today, mostly in iPhone programs and 64-bit programs on Mac OS X V10.5 and later.

So how does Runtime get initiated during a program run? There are now three ways to tune the Runtime;

1. Directly adjust the method from OC level

2. Call API through NSObject layer

3. Through objC’s underlying API

From this picture we can get an idea of the hierarchy of our code once it is up and running

This picture shows the 30% different ways in which the Runtime is tuned. The method of directly adjusting from OC level; IsKindofClass; It’s calling the API through the NSObject layer; Class_getInstanceSize; Is through objC’s underlying API;

Second, validate the Runtime with code

Here we create a CYFPerson class and declare two methods, sayNB and sayHello, in this class.

SayNB is implemented. SayHello is not implemented.

At this point, we use command+B to compile the project, and we find that the compilation is successful, but when we use command+R to run the project, we find that the program crash, and an unrecognized selector error;

Therefore, we can see from the above flow that the error occurred at runtime, not compile time, which is the difference between runtime and compile time;

What does the bottom layer do when the system calls a method?

During our development, we often call our own function methods when we execute [Person sayNB]; So what does the Runtime do for us after this line of code? We can use clang-rewrite-objc main.m-o-main. CPP to view the converted code as follows:

As you can see from the figure, the Runtime mechanism converts our code to objec_msgSend(message receiver, message body (sel+ argument)) after we call the method.

Now, is there a way to think about it, if we just call the system API, objc_msgSend(person, @selector(sayNB)); Whether it can achieve the result we want, after testing, it is completely ok, so we can get the conclusion that “call method = message send”;

But there is a case where we call a method that does not exist in the subclass, but exists in the parent class. After investigation, we can see that there is objc_msgSendSuper() in the Runtime. This interface can be called by: At this point we can access the methods of the parent class in the subclass;

conclusion

From the above process we can see that the essence of the method is message sending;