A simple understanding of IOS Runtime

Source: Internet
Author: User

Recently idle down, have time to study the runtime of the operating mechanism, before the development of the time has not been particularly concerned about this thing, just know there, but very little to deliberate use, the study is not very deep, simply say it, there are errors in the place welcome to correct at any time.

What is Objective-c Runtime?

Objective-c's runtime is a runtime library, which is a library written primarily using C and sinks, adding the ability to face objects to C and creating objective-c. This means that it is loaded in class information, all methods are distributed, method forwarded, and so on. OBJECTIVE-C runtime creates all the required structures, making OBJECTIVE-C's face object programming possible

(the previous paragraph is quoted from: http://www.cocoachina.com/iOS/20141008/9844.html)

The previous understanding of runtime is only limited to the type of the variable or object that was deferred to the runtime at compile time. For example, define a variable of type nsstring, the statement writes

The output will be

Obviously, when we define the variable str of the nssring type, its type is not determined, but it waits until it is run to determine the object's embodiment.

But this is not the point to say today, if the operation is only this function, then the Apple engineers are too overqualified, the operation of the most important role is reflected in the message mechanism.

As we all know, OC is an object-oriented language, and many times it is in the object-oriented perspective when it comes to solving problems. For example, I want to build a house, I do not need to consider how to build, just consider the task of a few people to do, such as a production window, b production door, c production roof .... I just have to think about assigning these tasks to a few people, and I don't need to think about how they're going to work out. Just give it a assignment, call the following method:

This assigns the task to a, and the compiled statement at run time is as follows

Objc_msgsend (Apeple, @selector (Makewindow));

convert [Apeople Makewindow] to the format of the Objc_msgsend function, the function has two parameters, the first parameter is the object we created, the second parameter is a method selector, using @selector (Makewindow) To generate a Sel,sel is a wrapper over the method. The SEL type data of the wrapper it corresponds to the corresponding method address, and the method address can be called. Then according to the SEL to Apeople belong to the class to find, we first look at the structure of class in OC, in fact, class is a pointer to the structure, the content is as follows:

The following is a detailed explanation of the parameters, the code part from the runtime source code, quoted: http://blog.csdn.net/uxyheaven/article/details/38113901

Version information for the versions class, which defaults to 0info some bit IDs for the run-time useinstance_size The instance variable size of the classAn array of Ivars member variables 
    1. struct Objc_ivar_list {
    2. int ivar_count;
    3. / * Variable length structure * /
    4. struct Objc_ivar ivar_list[1];
    5. }
Array of methodlists method definitions
    1. struct Objc_method_list {
    2. struct objc_method_list *obsolete;
    3. int method_count;
    4. / * Variable length structure * /
    5. struct Objc_method method_list[1];
    6. }
Objc_cache points to the most recently used method. Optimized for method invocation.
    1. struct Objc_cache {
    2. unsigned int mask/ * Total = mask + 1 */;
    3. unsigned int occupied;
    4. Method buckets[1];
    5. };
An array of protocols protocols
    1. struct Objc_protocol_list {
    2. struct objc_protocol_list *next;
    3. Long Count;
    4. Protocol *list[1];
    5. };

(End of quote)

When the Objc_msgsend function is called, the SEL generated by @selector (Makewindow) is used to go to the cache of class to find the pointer to the function, and if it is found here, it is called directly; Then go to MethodList to find the pointer to the function, find the words to call directly, did not find the words to go to super_class find, after finding the function pointer cached to the cache to facilitate the next search.

The above is my understanding of the operation of the message sending mechanism, now understand is relatively shallow, not deep enough, and so on later further study, will be more content to share. The next article will introduce the places that runtime often uses in development.

A simple understanding of IOS Runtime

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.