Today we will focus on an interview question to analyze the answer from the source point of view!
Is there a load method in a Category? When is the load method called? Can the load method inherit? How do you load the order if there is inheritance?
First we first look at the following code, we first look at the results of the code, and then from the source analysis!
NSLog(@”GDPerson+load”); NSLog(@”GDPerson+Test1+load”); NSLog(@”GDPerson+Test2+load”); }
1.GDPerson+Test1 and GDPerson+Test2 are GDPerson categories.
2.GDStudent+Test1 and GDStudent+Test2 are GDStudent classifications.
3.GDStudent inherits from GDPerson
From the above results we can see that the load method is called when the classification is loaded. There is no operation, it is called actively.
GDPerson and GDPerson+Test1 and GDStudent+Test2 call order and compile order?
See the screenshot below:
No matter how we adjust the compile order, the GDPerson load method is always printed first, and the remaining two classes are loaded in the same order.
GDPerson +Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2: GDStudent+Test2
thinking
GDPerson + (void) test = GDPerson + (void) test = GDPerson + (void) test = GDPerson Here we can take a look at what methods are stored in metaclass objects. Remember my previous blog post? There is a method to get methods from objects (exploring the nature of KVO (2)) that can be viewed here.
Let me paste it again:
As you can see here, the load method is incorporated into the metaclass object, which is consistent with our previous blog post about the knowing-point
Peep through the source code to prove our conclusion
Objc4 source code objC4 source code objC4 source code
I will first view the source code order affixed, and then look at it with you.
Objc-os. mm is the program entry, _objc_init initializes; Load_image, image stands for image, load_image is exactly the load method we want, and I’m going to walk through it step by step
The load method of the class is called first, and the load method of the class is called later. How does call_class_loads get called
Class object (isa) is used to find the metaclass. The metaclass (isa) is used to find the metaclass. And so on, so the load method is called directly at runtime.
The same is true of the classification method, and you can see it for yourself, but I won’t say it here. I believe you all have the answer in mind.
Now let’s look at a more complicated case
What if there is inheritance?
GDStudent = GDPerson; GDStudent = test1; GDStudent = test2
No matter how we change the compile order, the parent class load method will be called first, then call this class, and then call other classes according to the compile order, which you can verify yourself, and then use source code analysis to see if this is the case.
An inherited source code analysis call sequence appears
So let’s look at the implementation of the call_class_loads method
So we’re going to look at the order of classes, and before we call it, there’s a prepare_load_methods method, which is probably the order of classes, so let’s go in and look at schedule_class_load, Schedule our class load, so go ahead and see (it’s a lot of work, it’s a little bit easier, I’ve marked it clearly, it’s easy to try), see the figure below
As you can see from the diagram above, the order of the array is the parent class first, and schedule_class_load is a recursive call that calls all the parent classes, so it is loaded first. Let’s take a look at the implementation of add_class_to_loadable_list(CLS)
Having said so much, we can finally conclude 😄
Conclusion:
The +load method is called when the Runtime loads classes and classes
Each class or category is invoked only once during program execution
Call order:
1. The load method of the class is called first;
2. Call the load method of the subclass first before calling the load method of the parent class.
3. Compile in order (first compiled, first called)
4. Then call the load method of the classification as follows: compile first, call first
Next we answer the interview questions
Load method inheritance is relatively simple, I will not say, it is also a message mechanism, through isa, so it will call the load class first, this you try yourself!
Is there a load method in a Category? When is the load method called? Can the load method inherit?
There’s a load method.
The load method is called when the Runtime loads a class or class
The load method can be inherited, but usually the system does not call the load method automatically
In the next part of my blog I will cover the other basics of the iOS Category.
If you find my writing helpful, please follow me and I will continue to update 😄
= = =