-
Method – Swizzling is what
Method-swizzling means method exchange. Its main function is to replace the implementation of one method with the implementation of another method at runtime, which is often referred to as iOS dark magic. (The so-called exchange is to change sel and IMP correspondence as shown in the following figure)
-
Method-swizzling related Api interpretation
class_getInstanceMethod
Get instance methods through SELclass_getClassMethod
Get the class method through selmethod_getImplementation
Gets the implementation of a methodmethod_setImplementation
Sets the implementation of a methodmethod_getTypeEncoding
Gets the encoding type implemented by the methodclass_addMethod
Add method implementationclass_replaceMethod
Replace the implementation of one method with the implementation of another methodmethod_exchangeImplementations
Swap the implementations of the two methods
-
Method – Swizzling pit
-
Execute once problem
Imagine if the method swap is performed twice and the result is that the first swap is done correctly, but the second swap will restore the result of the first swap, so the general mehod- Swizzling is written in
load
Method, butload
Method can not help but think to continue to call, so it is possible to call many times, in view of this week’s situation can be through the singleton design principle, to achieve method transesterification once the following code: -
A problem where a subclass of a method needs to be swapped but the parent class does
The sample parent class contains
personInstanceMethod
Method subclass containslg_studentInstanceMethod
The method now swaps the two methods (we know that if we don’t find the implementation of the method in a subclass we go back to the parent class so the result is that the parent class’s method and the subclass’s method are swapped) as shown belowCorresponding code implementationShould contain in a subclasslg_studentInstanceMethod
The implementation of the subclass is calledpersonInstanceMethod
Okay, but the superclass doesn’tlg_studentInstanceMethod
The implementation of a method that looks up the method only looks up the parent class and doesn’t look up the list of methods in the child class so it crashes when the parent class calls it Corresponding solutions:
Before switching, check whether the current class has a corresponding method implementation. If not, the method will be replaced. If there is, the method will be swapped as followsOf course, if the method to be swapped does not exist in the current class, you can add an empty implementation and then swap the method. Either of these options can be customized according to the requirements of the project
Solution optimization
And of course there are incoming ones -
The subclass doesn’t implement the parent class
This kind of situation
class_getInstanceMethod
I don’t get the value, so we just need to determineclass_getInstanceMethod
If yes, add an empty method to implement the procedure and then go through the above steps in the following code
-