In this blog post we will explore these issues.

1.+initialize method

2. What is the difference between load and initialize methods? In what order are they called in a category? And how they call each other when inheritance occurs?

Let’s see if Initialize is called when the Runtime loads a class or class, like Load.

Once again, I created the GDPerson class and the Test1 and Test2 classes, calling initialize, respectively, as follows:

Initialize is not called, and load is not called by initialize. We can tell the difference between initialize and load.

The + Initialize method is called the first time the class receives a message

For example, I alloc GDPerson [GDPerson alloc]; So let’s see what happens

If you are careful, you may notice that this is not very similar to our previous class method call. In fact, it is the same. It is also sent through a message mechanism like objc_msgSend([GDPerson) Class],@selector(initialize)), if the class has the same name as the class method, it calls the class method first, and it’s postcompiled, and it calls the same method first.

What happens if there is inheritance?

GDStudent+initialize class GDStudent+initialize class

Initialize calls the parent class initialize first, and then the child class initialize. Each class is initialized only once

+initialize may be called more than once

Now I subclass GDCat, inherit GDStudent, and remove +(void)initialize from GDStudent. Let’s run it again and see what happens.

GDStudent will call +initialize when it initializes, and it will come to the metaclass object we learned earlier. First, find GDStudent’s metaclass object through ISA, and find +initialize in the metaclass object. If it cannot find +initialize, it will pass super GDPerson: GDPerson: GDPerson: GDPerson: GDPerson: GDPerson: GDPerson: GDPerson: GDPerson: GDPerson: GDPerson: GDPerson Here we have another example of initialize

Source code analysis

(Several previous blogs are introduced source download address)

The +initialize method is called when the message is sent, so let’s go to the objc_msgSend method and see if the +initialize method is used.

Here we find that all assembly language, report language will not waste your time, we through another way to think,class_getInstanceMethod method to view, I see the latest version of the source is objC4-818 version, it is not like the previous version can see some implementation, and objC4-818 version This I see most implementation is assembly language, so can not be specific analysis, here we can download a previous version of the analysis, are very clear, I put the source analysis steps hair, interested can try

(I’ll add if I can see it later).

Conclusion:

1.+initialize method

1. The +initialize method is called the first time the class receives a message

Call +initialize from parent class and then +initialize from subclass.

3. If the subclass does not implement +initialize, +initialize of the parent class will be called (so +initialize may be called multiple times).

4. If the class implements +initialize, it overrides the class’s own +initialize call

2. What is the difference between load and initialize methods?

Load is called from the address of the function. Initialize is called from objc_msgSend.

Load is called once when the Runtime loads the class or class, while Initialize is called once when the runtime receives the message. Each class is initialized only once, and its parent class may be initialized multiple times.

Load: call the class first. The parent class will be called before calling the subclass. The first compiled class will be called first.

Then call the classification, also the first compilation of the classification call first;

Initialize: Initialize the parent class first, and then initialize the child class, possibly by calling the parent class initialize several times.

In the next part of this blog I will cover the basics of iOS association objects.

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