Method Swizzling and methodswizzling

Source: Internet
Author: User

Method Swizzling and methodswizzling

I. Concepts

Method allocation: Objective-C is the runtime language, that is, the method to be called must be parsed at runtime. We can also change the sub-name at runtime. In this way, we neither need to view the source code, nor need to rewrite the subclass to override the method to change the function of the class itself. In this way, the new function will be displayed in all instances of the class, not limited to the instances that override the subclass. This solution is called method swizzling ).

IMP: the class method list maps the sub-names to the implementation of the method, so that the dynamic message dispatching system can find the method to be called Based on the times, these methods are expressed in the form of pointers, which are called IMP. The prototype is as follows:

id(* IMP)(id,SEL,.....)

SEL: the selector is used to indicate the name of a method at runtime. A method selector is a C string registered to (or mapped to) Objective-C at runtime, it is generated by the compiler and automatically mapped by the system during class loading.

A Class maintains a dispatch table used to parse messages sent during running. Each object (entry) in the scheduling table is a Method ), the key value is a unique name -- selector (SEL), which corresponds to an Implementation (IMP) -- actually a pointer to a standard C function.

II. Implementation of method allocation

First, take a look at two images:

With NSString 2-3, you can select the child lowercaseString, uppercaseString, and capitalizedString. Each selection child is mapped to a different IMP. Now we can change the Selection Sub- ing table to the 2-4 ing table shown in 2-4 by running the system.

To put it simply, we have modified the layout of the method instead of rewriting the subclass or others, which will be reflected on all NSString instances. The following are some basic function operations and functions:

① Implementation of exchange methods

void method_exchangeImplementations(Method m1,Method m2)

The parameters of this method are implemented by two methods, which can be obtained through the following functions:

Method class_getInstanceMethod(Class class,SEL aSelector)

Method to extract the corresponding method from the class according to the given selection. Here is an example of lowcaseString and uppercaseString exchange:

Method originMethod = class_getInstanceMethod([NSString class],@selector(lowcaseString));Method swipMethod = class_getInstanceMethod([NSString class],@selector(uppercaseString));method_exchangeImplementations(originMethod,swipMethod);

If we call the NSString lowcaseString, the implementation result is the implementation of uppercaseString. (That is, if the lower-case conversion is called, the upper-case conversion is used ).

Direct Exchange is of little significance. We generally use it to rewrite a class method, add our own stuff, and then exchange this method with the existing methods of the class.

② Add a new method:

  + (IMP)swizzleSelector:(SEL)origSelector                  withIMP:(IMP)newIMP {    Class class = [self class];    Method origMethod = class_getInstanceMethod(class,                                                origSelector);    IMP origIMP = method_getImplementation(origMethod);        if(!class_addMethod(self, origSelector, newIMP,                        method_getTypeEncoding(origMethod)))    {      method_setImplementation(origMethod, newIMP);    }        return origIMP;  }  

I will not introduce it here. You know ,,,,

Iii. Summary:

1. You can add or replace the sub-corresponding method during running.

2. Use another implementation to replace the original implementation. This program is called method allocation.

3. Pay attention to usage. Generally, method allocation is performed only when you debug the program. Few directly modify the functions of a class outside the debugging program.

Iv. Conclusion:

Method swizzling will introduce so much and hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.