1. About runtime I have used runtime to solve the problem of changing the global font in a project before, so I once again felt the power of runtime black magic. Now I have the opportunity to share some of my understanding of runtime. Object calling method is a frequently used function in Objective-C, that is, message passing. Objective-C is a superset of C, so unlike C, Objective-C uses dynamic binding, that is, runtime. I won't say much about Objective-C's message passing and message mechanism. Today I will mainly talk about dynamic methods, that is, function calling. 2. Several related functions The following figure summarizes in detail the order of each function call and the prerequisites for execution Message passing function calls 1. When an object receives a message that it cannot interpret, it first calls the
This method will be executed at runtime if the IML of SEL is not found. This function gives the class the opportunity to add functions using class_addMethod. According to the documentation, if the added function code is implemented, it returns YES, and if it is not implemented, it returns NO. For example, I created a new project. First, I executed the doSomething1 method in the ViewController class. The code is as follows
Operation Results
As expected, the program crashes because the doSomething method is not found. Next, we implement the + (BOOL)resolveInstanceMethod:(SEL)sel method in it and determine if SEL is doSomething and output add method here
Continue running and see the log
You can see that the program still crashes, but we can see that add method here is output, which means that our + (BOOL)resolveInstanceMethod:(SEL)sel method has been executed and entered the judgment. Therefore, here, we can do something to make this method respond so that it will not crash in the ***- (void)doesNotRecognizeSelector:(SEL)aSelector method. Next, we continue to operate as follows
Imported and executed the class_addMethod method in + (BOOL)resolveInstanceMethod:(SEL)sel, then defined a void dynamicMethodIMP (id self, SEL _cmd) function, ran the project, and looked at the log
At this time, we found that the program did not crash, and it also output doSomething SEL, which means that we have successfully added a method to our class through the runtime. The class_addMethod method is defined as follows
Then the method to add this function should be ass_addMethod([self class], @selector(newMethod), (IMP)newMethod, "i@:@"); 2. If the method is not found or added in + (BOOL)resolveInstanceMethod:(SEL)sel The message continues to be passed down to - (id)forwardingTargetForSelector:(SEL)aSelector to see if there is an object that can execute this method. Let's rebuild a project and create a new class called SecondViewController, which has a - (void)secondVCMethod method as follows
The project structure should be like this Project Catalog Now I want to call the - (void)secondVCMethod method in ViewController. We know that ViewController and SecondViewController have no inheritance relationship. If we follow the normal steps, the program will definitely crash directly because it cannot find the - (void)secondVCMethod method in ViewController.
Operation Results
Now let's process this message as follows
Operation Results
We will find that - (void)secondVCMethod is executed and the program does not crash. The reason is that at this step
When the - (void)secondVCMethod method is not found, the message continues to be passed until - (id)forwardingTargetForSelector:(SEL)aSelector, and then I create a SecondViewController object in it, and if this method exists, I return the SecondViewController object. This function is the message forwarding. Here we successfully pass the message to SecondViewController and let it execute, so the method is executed. At the same time, it is equivalent to completing a multiple inheritance! 3. *** Of course, there are several more functions, which are clearly expressed in the above picture. If you are interested, you can try it yourself to see what the order of message delivery is. The above-mentioned knowledge is just the tip of the iceberg of runtime. The power of runtime black magic is far more than this, such as method swizzling, which is still very useful in actual project combat. I will introduce it later when I have time. refer to
|
<<: Ask the App Store Beginner's Guide Everything you need to know is here!
>>: Apple vs. FBI: Not as simple as black and white
Speaking of the four words "add fans on WeCh...
"If girls don't sleep past 12 o'cloc...
There is a physical indicator whose name is diffi...
Beijing News Express According to the official We...
As the old saying goes, "30% of the test and...
Drinking water is an essential part of our daily l...
Who would refuse a hot pot invitation? From the i...
After working in advertising for so many years, w...
Recently, the news that a 2-year-old child died a...
1. Market Analysis Commercial vehicle market: Dem...
Jian Yang's "Wanjian Guizong Daily Limit...
Dapeng Education-AI Case Enhancement Course Resou...
Article introduction: This article mainly introdu...
If you want to save all the faces of your loved o...
How much does it cost to attract investment throu...