Lattice 6: I understand runtime (1)

Source: Internet
Author: User

Basic introduction

1, according to official documents, OC has a feature: it will try to postpone some decisions from compile time and link to run time, so the language needs more than just a compiler, it also needs a runtime system to handle those already compiled code.

2, runtime has two kinds: legacy runtime and Modern runtime, the difference is:

(1), in Legacy Runtime, if you modify the instance variable of a class, you need to recompile;

(2), in modern runtime, in this case you do not need to recompile again.

3, OC program and runtime system has three levels of interactive mode:

(1), most of the time, when you directly use the OC code, the runtime system is in operation. One of the most important functions is message sending (Messaging);

(2), the use of some nsobject class methods, such as description or Iskindofclass:, Ismemberofclass: And so on (in fact, most of the classes are inherited from the NSObject, you can use these methods, There are also some inherited from other classes, such as inheriting from the Nsproxy class);

(3), directly invoke the method provided by the runtime.

Message Sending ( Messaging ) Concept of

4, before discussing the message to send, we must first understand a concept: what is the selector of the method?

The selector of the method is a key calculated from the method name of this method, and the key can be used to get the method name of the method. Since selector is based on the method name, the selector of the method with the same name are the same.

In a class, in order for a selector to uniquely identify a method, it is not allowed to have two identically named methods in the same class, even if the arguments are different. (So OC has no method overloads)

Methods in addition to selector, there is another component is the method implementation (procedure, also known as method implementation), refers to the implementation of the Code.

So it can be understood that the method is the selector and the implementation of the collectively.

At the same time, it should be noted that the selector and implementation of a method is not a fixed correspondence, the process of sending a message is actually through the selector of the method to find its implementation process.

5, the basic process of message sending:

In OC, when we "call" the method, we use:

[Receiver message]

In this syntax, the code does not immediately execute the code of the message method, and there is a process of sending (Messaging) messages between this code and the code of the message method.

The compiler compiles this code into:

Objc_msgsend (receiver, selector)

If the method has more than one parameter, it is compiled with parameters:

Objc_msgsend (receiver, selector, arg1, arg2, ...)

The Objc_msgsend () function then executes the process of sending the message.

The message is actually the objc_msgsend () of all parameters of the function, which can be understood as: The message is a collection of details such as the method caller, the selector of the method, and the parameters of the method.

The process of sending a message is actually the objc_msgsend () function that finds the corresponding method implementation code (procedure, which is the method implementation) and executes it according to the receiver and selector parameters.

In this process, how does the Objc_msgsend () function find the corresponding class and find the corresponding method to implement it? And what happens if something goes wrong in the process of finding it?

Before we look at these issues, let's look at some data structures related to runtime.

Runtime Related data Structures

6, we start from the definition of objc_msgsend (), to study these data structures, objc_msgsend () is defined as follows:

ID objc_msgsend (ID self, SEL op, ...)

It has two main parameters, one for the ID type, and one for the SEL type.

(1), first look at the SEL type, we can find it is defined as follows:

No further definition of struct objc_selector is found, but we can literally know that the struct is a method, or some information that uniquely identifies the method, and the SEL as a pointer to selector can be understood as a pointer to a method;

Another main parameter of (2), Objc_msgsend () is the ID type, and we can find the definition of the ID type as follows:

So the ID is actually a pointer to struct objc_object struct, and struct objc_object This struct contains a member Isa, the data type of this member is class, then let's see what class is.

(3), class is defined as follows:

As we can see, class is a pointer to struct objc_class struct, and further find the definition of struct objc_class struct, as follows:

You can see that the struct objc_class struct contains a lot of members, and we look at these members one by one.

7, struct Objc_class:

(1), first of all we can magically find that struct objc_class struct the first member of the data type is also class, from above we already know that class is a pointer to struct objc_class struct, so the struct OBJC The first member of the _class struct is also a struct objc_class struct called Isa.

This shows that the class itself is also an instance of some kind. This first press not the table, in the following (9) again to do the discussion;

(2), struct objc_class struct (hereinafter referred to as the "class") the second member is still a class type of data Super_class, representing the structure of the object corresponding to the parent class;

(3), version and info are stored class-related information, when the instance variable of a class changes, its version of the content will be changed;

(4), instance_size is the size of the instance object generated by this class;

(5), the member Ivars pointer points to the struct objc_ivar_list struct type data, we can find the struct objc_ivar_list structure this data definition:

You can see that the most important member is an array of struct objc_ivar struct ivar_list[], which is an array of all the instance variables contained in this class.

We can also look further at the definition of struct objc_ivar struct:

So in fact a struct objc_ivar struct is a member variable;

(6), Members Methodlists and Ivars are similar, let's look at the definition of a struct objc_method_list this data type:

Structure and struct objc_ivar_list are basically similar, the main content is also an array, composed of struct objc_method type of data, we look at the definition of struct Objc_method:

It can be found that the data of struct Objc_method type is actually the method. The most important member of this struct is an IMP type data method_imp, which is actually a pointer to the method implementation code.

At the same time, it can be noted that member Methodlists is a level two pointer, which determines that it can change the point of the *methodlists, that is, can modify the method list, which is the category can be added to the cause of the method;

(7), the member cache is used to store the frequently called method, when calling methods on an object, runtime will find the object corresponding class according to the object's Isa pointer, and then first in the cache of the class to check the method to be called, when the cache is not found, Will go to the methodlists to find;

(8), the member protocols identifies this class to follow the agreement;

(9), said before, the class itself is also a kind of instance, we from class The first member Isa is also a class type of data out of this conclusion.

The class to which the class belongs is called the Meta class, which stores all the class methods of a class, which is an instance of the Meta class. When we call the class method, we actually send the message to it as an instance: when we send a message to an object, runtime looks for the method in the list of methods of the class to which the object belongs, and when a message is sent to a class, it is looked up in the list of methods of the class's meta-class.

Continue to dig deeper, found that the meta-class is also a class, then its Isa pointer to where to point? This continues to be endless, so all the meta-class Isa pointers will be pointed to the same class-The base class (Root classes) of the meta-class, the base class of the meta-class of the ISA pointer to its own, forming a closed loop.

A concept needs to be clear: the Meta class and the parent class (super Class) are two different concepts, and generally a class will have a meta class and a parent class.

A meta-class is the class to which the class is treated as an object, and the parent class is the class that this class inherits from when it is treated as a class.

Their relationship is as follows:

Note that the base class (Root Class) is actually NSObject, and its superclass points to nil. The parent-class pointer to the NSObject's meta-class points to nsobject, eventually forming a loop at the top.

Lattice 6: I understand runtime (1)

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.