IOS development diary 17-runtime basics, ios17-runtime

Source: Internet
Author: User

IOS development diary 17-runtime basics, ios17-runtime

Today, the blogger has a basic runtime requirement and encountered some difficulties. I would like to share with you the hope to make common progress.

Objective-C runtime is a runtime Library mainly written in C and assembly languages. It creates Objective-C by adding object-oriented capabilities to C languages. This means that it can load class information, distribute methods, forward methods, and so on. During Objective-C running, the most important thing is to provide all the basic support for the implementation of object-oriented features of Objective-C language.

 

So what exactly is the runtime? I believe that Baidu has read many articles and found that nine of the ten articles do not understand them. At this time, we do not know many basic things. Today, I will share with you the runtime basics.

The code we write will be converted to runtime C code execution during the program running, for example, [xxxxxx doSomething]; will be converted to objc_msgSend (xxxxxx, @ selector (doSomething ));

In OC, everything is designed as an object. We all know that a class is initialized into an instance, and this instance is an object. In fact, a class is essentially an object, represented by a struct in runtime.

 

Related definitions:

/// Description of a Method in the class typedef struct objc_method * Method; // instance variable typedef struct objc_ivar * Ivar; // Category Categorytypedef struct objc_category * Category; /// Type Def struct objc_property * objc_property_t declared in the class;

Class representation in runtime

// Indicates the struct objc_class {Class isa; // pointer in the runtime, as the name suggests, indicating what it is. // The instance's isa points to the Class object, the isa of the Class Object points to the metaclass # if! _ OBJC2 _ Class super_class; // point to the parent Class const char * name; // Class name long version; long info; long instance_size struct objc_ivar_list * ivars // member variable list struct objc_method_list ** methodLists; // method list struct objc_cache * cache; // cache // an optimization, the called method is saved to the cache list. For the next call, find the cached struct objc_protocol_list * protocols // protocol list # endif} OBJC2_UNAVAILABLE;/* Use 'class' instead of 'struct objc_class *'*/

I believe many readers will be eager to turn off the screen when they see it. I will share with you the isa pointer and meta class (meta class)

Each object has a class to which it belongs. This is the basic concept of object-oriented, but in OC, this is effective for all data structures. Any data structure, as long as a pointer points to a class at an appropriate position, can be considered as an object.
In OC, which class an object belongs to is directed by its isa pointer. This isa Pointer Points to the class to which the object belongs.

Isa: A Class pointer. each instance object has an isa pointer pointing to the object Class, and the Class also has an isa pointer pointing to meteClass ). The metadata class saves the list of class methods. When a class method is called, it first looks for the implementation of the class method. If no, the Meta class will look for the method from its parent class. Note the following:MeteClass is also a class and an object.The Meta class also has an isa pointer. Its isa pointer eventually points to a root meteClass. The isa pointer of the root Meta class points to itself, forming a closed internal loop.

An OC class is actually an object, which means you can send messages to a class.
NSStringEncoding defaultStringEncoding = [NSString defaultStringEncoding];
In this example, defaultStringEncoding is sent to the NSString class. Because each OC class is also an object. That is to say, the data structure of the Class must start with the isa pointer and be fully compatible with the objc_object at the binary level. Then the next field of a class structure must be a pointer to the super class (or nil, for the base class ).
There are many methods for defining a class, depending on your Runtime Library version. However, either method uses an isa as the first field, followed by the superclass field.

To call class methods, the isa pointer of this class must point to a class struct containing these class methods.
This introduces the concept of meta-class: meta-class is a class of class objects.
Below is a simple explanation:
When you send a message to an object, runtime searches for the method list of the class to which the object belongs.
When you send a message to a class, runtime searches for the message in the meta-class method list of the class.

Meta-class is important because it stores all class methods of a class. Each class has a separate meta-class, because the class methods of each class are basically not identical.
Meta-class, like a Class, is also an object. You can still send messages to it to call the function. Naturally, meta-class also has an isa pointer pointing to its class. All meta-classes use the meta-class of the base class as their classes. Specifically, meta-class in any NSObject inheritance system uses the meta-class of NSObject as its own class.

According to this rule, all meta-classes use the meta-class of the base class as their classes, and the meta-class of the base class also belongs to itself, that is to say, the isa pointer of the base class meta-class points to itself.
Just as a class points to its parent class using the super_class pointer, The super_class of meta-class points to the meta-class of super_class of the class. It is traced back to the meta-class of the base class. Its super_class is directed to the base class itself.

In this way, instances, classes, and meta-classes in the entire inheritance system are derived from the base classes in the inheritance system. For the NSObject inheritance system, the NSObject instance method is effective for all instances, classes, and meta-classes in the system; the NSObject class method is effective for all classes and meta-classes in the system.
Meta-class is a class of class objects. Each class has its own unique meta-class. All class objects do not belong to the same meta-class.

Meta-class must ensure that the class object has all the instances and methods of the class in the inheritance system and all the intermediate class methods in the inheritance system. For all classes in the NSObject inheritance system, the NSObject instance methods and Protocol methods must be effective for both them and their meta-class objects.
All meta-classes use the meta-class of the base class as their own base class. The meta-class of the top-level base class is the same, but it only points to itself.

Runtime application:

 

1. dynamically create a class (such as the underlying implementation of KVO)

 

2. dynamically Add a property \ method for a class and modify the property value \ Method

 

3. traverse all member variables (attributes) \ all methods of a class

 

Essentially, the above is achieved by obtaining the isa pointer of an object or class through relevant methods.

 

Related functions

 

1. Add

 

Add function: class_addMethod

 

Add instance variable: class_addIvar

 

Add attribute: @ dynamic label or class_addMethod, because the attribute is actually composed of the getter and setter functions.

 

Add Protocol: class_addProtocol (to be honest, I really don't know how to add a protocol dynamically ,-_-!!)

 

2. Get

 

Obtain the function list and information of each function (function pointer, function name, etc.): class_getClassMethod method_getName...

 

Obtain the attribute list and information of each attribute: class_copyPropertyList property_getName

 

Obtain information about the class, such as the class name: class_getName class_getInstanceSize

 

Get the Variable list and variable information: class_copyIvarList

 

Get the variable value

 

3. Replace

 

Replace the instance with another class: object_setClass

 

Definition of the replacement class method: class_replaceMethod

 

4. Other common methods:

 

Implement the exchange of two methods: method_exchangeImplementations.

 

Set the implementation of a method: method_setImplementation.

 
There are several more articles to increase your understanding of runtime.
Http://quotation.github.io/objc/2015/05/21/objc-runtime-ivar-access.html
Http://www.jianshu.com/p/425a39d43d16? Utm_campaign = maleskine & utm_content = note & utm_medium = writer_share & utm_source = weibo
Http://mp.weixin.qq.com? _ Biz = MjM5NTIyNTUyMQ ==& mid = 208927760 & idx = 1 & sn = 30b9caecba709553e463d719668454ae & scene = 2 & from = timeline & isappinstalled = 0 # rd

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.