You may have seen many friends introduce the concept of IMP pointer in Runtime related articles, so what is the actual role of IMP? Let's start with a function. Method Swizzling If you have some knowledge of Runtime, you must have heard of or used this function:
It is usually called method swizzling, which is considered the "black magic" of ObjC. Its function is to dynamically swap the implementation of two methods during program execution. For example, there is such a usage scenario: There are many ViewControllers in our program. I want to print out the name of each Controller in the console after ViewDidLoad is executed with minimal changes to the project, so that I can debug or understand the project structure. Many friends may ask, isn't it enough to let all controllers inherit from a BaseController? I want to explain the disadvantages of doing so here: if there are many Controllers in your project, you will need to modify every Controller in the project that does not inherit from BaseController, and changing the hierarchy at will may cause unexpected errors. Actually, our goal is to override the ViewDidLoad method and add a few Logs at the end of its method, so we need to create a category for UIViewController, because we know that if we override a method in Catagory, it will overwrite its original method implementation, but after doing so, there is no way to call the original system method, because calling your own method in a method will be an infinite loop. So our solution is to write another method to "exchange" with viewDidLoad, so that the external call to viewDidLoad will be transferred to the newly created method, and similarly, when we call the newly created method, it will be called to the system viewDidLoad. IMP pointer In fact, there is a simpler way that allows us to achieve the same purpose, using the IMP pointer. IMP is the abbreviation of Implementation. As the name suggests, it is a pointer to a method implementation. Each method has a corresponding IMP, so we can directly call the IMP pointer of the method to avoid the problem of method call infinite loop. Calling an IMP is the same as calling a normal C function, for example:
However, if your project has not been configured otherwise, calling the compiler in this way will not work. Let's take a look at its definition first:
By default, your project opens this configuration. In this case, IMP is defined as a function with no parameters and no return value. So you need to search for this option in the project and turn it off. The trouble is that you need to modify the project configuration every time you use it, so here I will introduce another method: redefine a pointer type that is the same as the IMP pointer with parameters, and force it to this type when obtaining IMP. After using the IMP pointer in this way, there is no need to write a new method for ViewController: There is another point we need to pay attention to. If you call IMP directly in this way, the classic EXC_BAD_ACCESS error will occur. The IMP pointer we defined is a type with a return value, but in fact the viewDidLoad method we obtained has no return value, so we need to define a new function pointer of the same type as IMP, such as VIMP, and position its return value to Void. In this way, if the method you modify has a return value, use IMP, and if it has no return value, use VIMP. It is worth noting that if the method you override has a return value, don't forget to do return at the end. Summarize In fact, calling a method's IMP pointer directly is more efficient than calling the method itself, so if you have a suitable opportunity to get the method's IMP, you can try to call it. This is just one of the scenarios where IMP is used. It has many other uses. I hope you can discover more of them. |
<<: Swift version of infinite slideshow scrolling
In 2020, live streaming of goods via short videos...
Huawei App Market Brand Resource Bidding Promotio...
Generally speaking, the cost of attracting a new ...
Chutian Metropolis Daily, March 16 (Reporter Gao ...
1. Introduction SpriteKit is Apple's game dev...
As a public account editor with less than 100 mil...
OPPO search advertising resources rely on the ric...
Social network analysis (SNA) is a methodological...
Five years ago, a trend emerged - traditional ent...
"Today, we bring 5G to iPhone. This is an ep...
Friends who do promotion should all have this fee...
[[127723]] Beijing time, February 9 morning news,...
This article takes Toutiao as an example, taking ...
We spend a lot of time thinking about stream proc...
I often hear that there are four stages of learni...