Runtime 01-Classes and objects

Source: Internet
Author: User

On the question of "runtime mechanism", in order to understand the runtime in depth, first of all, starting from the most basic classes and objects, this article will explain in detail the hierarchy of classes and objects in OC, and the following will gradually update how to use the runtime operation class.
First, we find the definition of class and object from/usr/include/objc/objc.h and runtime.h:

1 //An opaque type, represents an Objective-c class.2typedefstructObjc_class *Class;3 ///represents an instance of a class.4 structObjc_object {5 Class Isa;6 };7 //A pointer to a instance of a class.8typedefstructObjc_object *ID;
Thus, the class is a pointer to the Objc_class struct, and the ID is a pointer to the Objc_object struct, where the member Isa is a pointer to the Objec_class struct body.
Let's take a look at the definition of Objc_class:
1 structObjc_class {2Class Isa;//point to Metaclass3Class Super_class;//point to Parent class4Constchar *name;//class name5     Longversion;//class version information, initialization defaults to 0, can be modified by the runtime function class_setversion or class_getversion, read6     LongInfo//Some identifying information, such as Cls_class (0X1L), indicates that the class is generic class, which contains instance methods and variables; Cls_meta (0x2l) indicates that the class is Metaclass, which contains class methods;7     LongInstance_size;//the instance variable size of the class (including instance variables inherited from the parent class);8     structObjc_ivar_list *ivars;//the address used to store each member variable9     structObjc_method_list **methodlists;//related to some flag bits of info, such as Cls_class (0x1l), the storage-class method, such as Cls_meta (0X2L), is stored as an instance method;Ten     structObjc_cache *cache;//a pointer to the nearest method used to improve efficiency; One     structObjc_protocol_list *protocols;//The protocol that stores this class declaration A}
It can be seen that the difference between a class and an object is simply that there are many more members in the structure of the analogy object, which are treated as a objec_object, that is, the class and object are objects, and in order to differentiate the concept, a term is introduced: class object Object), and instance objects (instance. Object) so that we can distinguish between objects and classes (don't confuse them).
Here is a detailed description of the members of the Objec_class:
The class structure pointed to by the ISA pointer in the Isa:objec_object (instance object) is called Class (that is, the class that the object belongs to), where the normal member variable is stored with the method of the dynamic method (remember "-")? ); Here the ISA pointer points to a class structure called Metaclass, which holds member variables of the static type and methods of the static type (the method that begins with "+").
Super_class: A pointer to the parent class of the class, and if the class is a root class (such as NSObject or Nsproxy), then Super_class is null.
Here we can see the inheritance hierarchy relationship between classes and objects in OC:
Note that all metaclass in the ISA pointer point to the root metaclass, while the root metaclass points to itself.  The root metaclass is generated by inheriting the root class, consistent with the members of the root class struct, and different from the ISA pointer of the root metaclass pointing to itself. When we call an instance method of an object, it finds the method first in the class methodlists that the ISA pointer points to itself, and if it is not found, the class object structure of the parent class is found through the class's Super_class pointer. The method is then looked up from the methodlists, and if it is still not found, it continues to be looked up through the Super_class parent structure, up to the root class;
When we call a class method, it will first find the metaclass through its own ISA pointer, and find the class method from the methodlists, if not found, it will pass through the Metaclass super_ The class pointer finds the Metaclass object structure of the parent class, and then finds the method from Methodlists, and if it is still not found, continues to look up through the Super_class to the parent class structure until the root metaclass;
After the above introduction, I believe you have a more profound understanding of the structure level of objects and classes in OC. We'll show you how to use the runtime mechanism later.

Runtime 01-Classes and objects

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.