Analysis of runtime Running Mechanism in iOS
http://blog.csdn.net/fuzheng0301/article/details/46898405
First, consider two questions:
First question,
1) What is the mechanism of runtime implementation, how to use it, and what is it generally used for?
I'm not going to beat around the bush with you on this question. I'll tell you directly.
runtime is a relatively low-level pure set C Language API, which belongs to a C language library, contains many low-level C language APIs.
In the OC code we usually write, when the program runs, it turns into the C language code of runtime. Runtime is the backstage worker of OC.
For example, in the following method of creating objects,
Examples:
OC :
[[MJPerson alloc] init]
runtime :
objc_msgSend(objc_msgSend("MJPerson" , "alloc"), "init")
Second question
What does runtime do? Where is it used? How to use it?
runtime belongs to the bottom of OC and can perform some very low-level operations (OC is unrealistic and difficult to implement).
-
In the process of running a program, create a class dynamically (such as the underlying implementation of KVO)
-
In the process of program running, add attribute method dynamically for a class, modify attribute value method
-
Traversing through all member variables (attributes) all methods of a class
For example, when we need to archive and archive the attributes of a class, there are many attributes. At this time, we will write many corresponding codes, but if we use runtime, we can set them dynamically.
2. To learn the runtime mechanism, we should first understand the following questions.
Objective-C It is a compiled and dynamic language (oc is a static type language emphasized here), which is common in development languages. Most of the general dynamic languages are interpretative languages. OC is a dynamic language even if it is compiled. That's because of the RunTime mechanism.
1. Related header files and functions
1 > Header file
- Using the header file, we can see the various methods in runtime!
2 > Relevant Applications
- NSCoding (archiving and archiving, using runtime to traverse all attributes of model objects)
- Dictionary --> Model (using runtime to traverse all attributes of the model object, extract the corresponding value from the dictionary according to the attribute name, and set it on the attributes of the model)
- KVO (using runtime to dynamically generate a class)
- For encapsulation frameworks (change as you want)
This is the direction of our runtime mechanism as long as it is applied.
3 > correlation function
1. class_copyPropertyList Gets a copy of the membership list array
2. Proper_getName gets the member name
3. class_getInstanceVariable to get Ivar of member object
4. object_getIvar takes values from Ivar objects
5. object_setIvar assignment function
6. objc_msgSend: Sending messages to objects
7. class_copyMethodList: Traversing through all methods of a class
8. class_copyIvarList: Traversing through all member variables of a class
9,class_.....
These are the functions we have to know when we learn runtime!
2. Necessary common sense
1 > Ivar: Membership variables
2 > Method: Membership Method
From the example above, we can see that the member variables we defined can be created dynamically using Method.
Third, we can see clearly the inheritance hierarchy between classes and objects in OC through a graph.
Note: The isa pointer in all metaclasses points to the root metaclass, while the root metaclass points to itself. The root metaclass is generated by inheriting the root class, which is identical to the members of the root class structure, except that the ISA pointer of the root metaclass points to itself.
1. When we call the object method of an object, it will first find the method in the class method Lists pointed by its isa pointer, and if it cannot find it, it will find its parent class through the class super_class pointer, and then find the method from its method Lists. If it still cannot find it, it will continue to use super_cl. Ass looks up the parent structure until the root class.
2. When we call a class method, it first finds the metaclass through its isa pointer and finds it from its methodLists. If it cannot find it, it finds the metaclass structure of the parent class through the super_class pointer of the metaclass. Then it finds the method from the methodLists. If it still can't find it, it finds the metaclass structure of the parent class. Continue to look up the parent structure of the upper class through super_class until the root metaclass;
Fourth, let's take a look at the code implementation:
1. Create a class
CustomClass.h
CustomClass.m
Next, create a category to implement the RunTime mechanism
NSObject+RunTimeTest.h
NSObject+RunTimeTest.m
test:
ViewController.m
Results Output: