Easy learning: the role of IMP pointer in Runtime

Easy learning: the role of IMP pointer in Runtime

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:

  1. void method_exchangeImplementations(Method m1, Method m2)

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:

  1. id returnObjc = someIMP(objc,SEL,params...);

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:

  1. if !OBJC_OLD_DISPATCH_PROTOTYPES
  2. typedef void (*IMP)( void   /* id, SEL, ... */ );
  3. else  
  4. typedef id (*IMP)(id, SEL, ...);
  5. endif

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

>>:  Unique and magical! What computer technology skills do programmers have that can make ordinary people feel magical?

Recommend

How to improve user loyalty to products?

This is a "red ocean era". Due to the r...

Code Review from Scratch

[[156391]] This post is not about introducing the...

How much does it cost to develop a candied fruit mini program in Anqing?

For entrepreneurs, although mini program developm...

Tips for effective advertising!

Everyone who has used competitive advertising kno...

What is the relationship between grabbing red envelopes and mobile phones?

On every day when people can legitimately grab re...

Director A talks about workplace power struggles

Director Lao A's course on workplace power st...

Zhao Yangang learned how to create millions of SE0 traffic in 60 days

The course schedule is as follows: ——/It Network/...

Why do we go to the bathroom more often when the weather gets cold?

Friends You should have had this experience. When...