A simple method for developing Runtime for ios, and developing runtime for ios

Source: Internet
Author: User

A simple method for developing Runtime for ios, and developing runtime for ios

In ios, the simple use of Runtime is essentially a C language function call at the bottom of the OC to send messages dynamically. Let's take an alloc init method as an example. We can call the runtime function to create an object.

   Person * p = objc_msgSend(objc_getClass("Person"),sel_registerName("alloc"));   objc_msgSend(p, sel_registerName("init"));
In fact, we can enter the following command on the terminal to view the implementation of the C language of OC, and we will find that the object creation is very similar to the above running time code.
clang -rewrite-objc main.m
Here, we also need to understand two conceptual problems: SEL and IMP. In fact, SEL is the number of the method, and IMP is the pointer to the method to be executed, we can find the specific implementation of this code through the method number.

We can exchange the methods of two objects below, where Method is the pointer to struct objc_method. In fact, SEL and IMP are equivalent to the title and page number in a book. We can know the page number through the title, and of course we can know the specific content based on the page number.

Method method1 = class_getInstanceMethod ([self class], @ selector (eat); Method method2 = class_getInstanceMethod ([self class], @ selector (sleep )); // method exchange method_exchangeImplementations (method1, method2 );

There are two methods that we need to know. One is to return to the following function when we fail to implement a class method we call.
+(BOOL)resolveClassMethod:(SEL)sel

The other is that when we implement the object method we call, the following function will be called if it is not found.
+(BOOL)resolveInstanceMethod:(SEL)sel
If you want to dynamically Add a function at runtime, you can add it here. Note that if the function to be written has parameters, then there are two parameters in front of the function: self and SEL _ cmd. You can change the name of current _ cmd. Self is actually the method caller, and SEL is actually the method number
+ (BOOL) resolveInstanceMethod :( SEL) sel {// dynamic addition method/* parameter 1 of the method: Yes parameter 2 of this class: method parameter 3 function pointer parameter 4 is the type of the returned value plus parameter */class_addMethod (self, sel, hello, ""); return [super resolveInstanceMethod: sel];} void hello (id self, SEL _ cmd, NSString * str1, NSString * str2) {NSLog (@ "% @ ------ % @", str1, str2 ); NSLog (@ "hello world ");}
Another thing to note is that the endless loop may not cause the program to crash directly, but the recursive call of the function will definitely cause the program to crash, because function execution has its own temporary stack, it will cause stack overflow and program crash.

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.