IOS-talking about runtime mechanism 01-class and object internal structure, ios-01-

Source: Internet
Author: User

IOS-talking about runtime mechanism 01-class and object internal structure, ios-01-

I have been studying the mechanism of runtime recently. I think there may be many people who are not quite clear about this problem, right? I will share with you my understanding of the runtime mechanism.

To understand runtime, first we need to understand the internal structure of classes and objects. Next we will first introduce the structural layers of classes and objects in OC.

1. First, find the definition of class and object in the runtime. h header file.
/// An opaque type that represents an Objective-C class.typedef struct objc_class *Class;/// Represents an instance of a class.struct objc_object {    Class isa;};/// A pointer to an instance of a class.typedef struct objc_object *id;

  

It can be seen that Class is a pointer to the objc_class struct, and id is a pointer to the objc_object struct. Its member isa is a pointer to the objec_class struct.

Ii. Let's take a look at the definition of objc_class in the header file.
Struct objc_class {Class isa; // points to metaclass Class super_class; // points to its parent Class const char * name; // Class name long version; // Class version information, the default initialization value is 0. You can use the runtime functions class_setVersion and class_getVersion to modify and read long info; // some identification information. For example, CLS_CLASS (0x1L) indicates that the class is a common class, including object methods and member variables; CLS_META (0x2L) indicates that the class is metaclass, including class methods; long instance_size; // instance variable size of this class (including instance variables inherited from the parent class); struct objc_ivar_list * ivars; // address used to store each member variable struct objc_method_list ** methodLists; // It is related to some flag bits of info, such as CLS_CLASS (0x1L). It stores object methods, such as CLS_META (0x2L), and struct objc_cache * cache; // pointer to the recently used method, used to improve efficiency; struct objc_protocol_list * protocols; // stores the protocols that this class complies}

It can be seen that many Members are added to the structure of the analogy object. The following describes the members of objec_class in detail:

Isa: the class structure pointed to by the isa pointer in objec_object is called class (that is, the class to which the object belongs ), it stores common member variables and object methods (Methods starting with "-"). However, the class structure pointed to by the isa pointer is called metaclass, it stores static member variables and static methods (Methods starting with "+ ).

Super_class: pointer to the parent class of the class. If the class is a root class (such as NSObject or NSProxy), super_class is NULL.

The following figure shows the hierarchy of classes and objects in OC:

Note: In all metaclass, the isa Pointer Points to the root metaclass, while the root metaclass points to itself. The root metaclass is generated by inheriting the root class, which is consistent with the root class struct. The difference is that the isa pointer of the root metaclass points to itself.

1. When we call an object method of an object, it first searches for the method in the class methodLists pointed to by its isa pointer, if the parent class cannot be found, the parent class will be found through the super_class pointer of the class, and then the method will be searched from its methodLists. If the parent class still cannot be found, the super_class will continue to be searched in the parent class structure at the upper level, until the root class;

2. When we call a class method, it first finds metaclass through its isa pointer and finds this class method from its methodLists, if no metaclass is found, the metaclass structure of the parent class will be found through the super_class pointer of metaclass, and the method will be searched from methodLists. If the method still cannot be found, then, the super_class class will be used to query the parent class struct until the root metaclass;

 

After the above introduction, I believe you have a further understanding of the structural hierarchy of objects and classes in OC. The following describes how to use the runtime mechanism.

Address: http://www.cnblogs.com/guoxiao/p/3583351.html

  

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.