IOS advanced development runtime (3), ios development runtime

Source: Internet
Author: User

IOS advanced development runtime (3), ios development runtime

3. Dynamic Addition Method

We can dynamically add methods through runtime. So what is dynamic addition? The dynamic addition method knows which method should be called only when the program runs. First, we need to understand this. When we write a piece of code and click run, the compiler will first perform the pre-compilation, compilation, link, and run steps. When the C language is re-compiled, the sequence of function calls has been determined. When the OC is compiling, it only determines the object and message to be sent, we do not know whether the corresponding method can be found for this message. We can determine whether the method we expect can be executed only at runtime. The following code is used as an example:

 

[Person test_inPerson]; // objc_msgSend (person, @ selector (test_inPerson); this function is obtained after the preceding statement is compiled. // Through compilation, we can know three points: 1. The message recipient is the person object. 2. Execute the method named test_inPerson. 3. This method does not contain parameters.

In my previous blog, I mentioned SEL and IMP. But since it was too late to write a blog, I didn't explain it seriously.

 

If you want to know the process of method calling, I am afraid we need to understand four concepts: (1) isa pointer. (2) superclass attributes. (3) SEL. (4) IMP.


1) isa pointer: pointer to the class of the object.

2) superclass: points to the parent class.

3) SEL: the selector is the ID generated based on the method name. Each selector is actually a char * type and records the position of the corresponding IMP. The SEL list itself is a set of Hash Storage, which is very efficient in searching.

4) IMP: function pointer.


The following describes the process of calling a method.

 

/* Call Method */[person test_inPerson];/* convert to message objc_msgSend (person, @ selector (test_inPerson); Compile the preceding statement to obtain this function. * // * 1) check whether the selector is empty. 2) check whether the person is empty. If the person is empty, set the selector to null. In this way, nothing is done, of course, no error is reported. 3) Search for IMP Based on SEL. First, search for the relevant IMP in the cache. If yes, execute. Otherwise, continue to the next step. 4) Search for the corresponding IMP in the IMP list based on the SEL and isa pointers. If it is found, execute. Otherwise, execute the next step. 5) Search for the IMP of the parent class based on superclass and SEL. If the IMP of the parent class is found, execute the command. Otherwise, continue this step until the NSObject class. 6) If no method is found in the NSObject class, an error is returned and the method cannot be found. */


After learning about the method call process, let's take a look at how to dynamically add methods. In order to be clear, we would like to give you the code here.

 

 

DZLPerson * person = [[DZLPerson alloc] init]; // you can call the test0 method to send a message, however, we do not implement this method in the person class and its classification [person handler mselector: @ selector (test0)];

 

# Import "DZLPerson. h" # import <objc/runtime. h> @ implementation DZLPerson/* Note that this function is not a method. Functions cannot be called through methods. */Void test0 () {NSLog (@ "test0 executed");}/* call this method if the class method cannot be found, determine whether to dynamically Add the Method * // + (BOOL) resolveClassMethod :( SEL) sel // {// return BOOL; ///}/* If the instance method cannot be found, call this method and decide whether to dynamically Add the Method */+ (BOOL) resolveInstanceMethod :( SEL) sel {// if test0 is not found, if ([NSStringFromSelector (sel) is1_tostring: @ "test0"]) {// Add the method. In fact, it is to connect the existing function Implementation (IMP) with SEL. Class_addMethod (self, sel, test0, "v @:");} return YES;} @ end


The printed result is

 

22:43:06. 406 runtimeExplanations[12452: 693059] test0Executed


The dynamic method is successfully added. The last note is that if functions and methods are not the same thing, don't confuse them. Methods are called through classes or objects, and functions can be directly called and executed.

 

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.