What is runtime
Runtime is a set of the underlying C language API (application programming Interface) contains a lot of powerful practical C language types, C language functions.
In fact, we write the OC code, the bottom layer is based on runtime implementation.
That is to say, the OC code that we usually write is turned into the bottom runtime code (c language code).
[obj say]-> Call ID objc_msgsend (ID self, SEL op, ...) Function------Obj.isa, class, Class.cache_list, class. Method_list-Superclass
For example [obj say]; When the object executes a method like this, it turns into the C language function in the runtime library (OBJC, @selector (say)) .
The inner object finds the method and executes the specific steps:
In the MRC development environment
1. First check whether @selector method can be omitted
2. Check if target object OBJC is nil, if nil, clearout and return directly
3. Locate the class for target and then find the IMP (function pointer) based on the @selector
4. First from the current class of a method called Cache_list cache list to find, jump to the corresponding IMP implementation method, if not found, go to the Method_list method list to find
5. If you don't find it, go to the parent's super_list, always find the base class (NSObject), and if you can't find it, go to the dynamic parsing and message forwarding mechanism.
the core of Duang~daung~daung runtime is dynamic analysis .
In this case, we know that the interior is like this. So is it possible to intercept all the system methods for our own use, as well as we want to master a class to be able to solve each of his instance variables and methods
What's the use of runtime?
When encountering a method that does not use inheritance to add a public property accessible to the system class, use the runtime to perfectly solve
1> can dynamically generate a class, a member variable, a method
2> can dynamically modify a class, a member variable, a method
3> can dynamically delete a class, a member variable, a method
Common functions, header files
#import <objc/runtime.h>://member variables, classes, methods
class_copyivarlist: Get all member variables inside a class
class_copymethodlist: Get all the methods inside a class
Class_getinstancemethod: Get a Specific instance method (object method, minus-start)
Class_getclassmethod: Get a specific class method (plus + start)
method_exchangeimplementations: Implementation of two methods of exchange
-----------------------------------------------------------
#import <objc/message.h>://message mechanism
objc_msgsend (...)
1 "Runtime implementation of the mechanism is what, how to use, generally used to do?"
I'm not going to mealy you with this question, just tell everyone,
Runtime is a relatively low level of pure C language API, belongs to 1 C language Library, contains a lot of the underlying C language API.
In the OC code that we usually write, the program runs the process, in fact, it turns into the runtime's C language code, runtime is OC's behind-the-scenes workers
For example, in the following method of creating an object,
Example:
Oc:
[[Mjperson alloc] init]
Runtime:
Objc_msgsend (Objc_msgsend ("Mjperson", "Alloc"), "Init")
A second question
What does runtime use for?? Where do you use them? How to use it?
Runtime is the bottom of the OC and can perform some very low-level operations (with OC is not realistic, bad implementation)
Dynamically create a class during the program's run (for example, the KVO implementation)
Dynamically add properties \ Methods to a class while the program is running, modify property values \ Methods
Traverse all member variables (properties) of a class \ All methods
For example: We need to archive the properties of a class when the properties are particularly large, when we will write a lot of corresponding code, but if you use the runtime can be set dynamically!
For example, the PYPerson.h file looks like this
Import
@interfacePYPerson: NSObject
@property (nonatomic, assign) int age;
@property (nonatomic, assign) int height;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age2;
@property (nonatomic, assign) int height2;
@property (nonatomic, assign) int age3;
@property (nonatomic, assign) int height3;
@property (nonatomic, assign) int age4;
@property (nonatomic, assign) int height4;
@end
and the contents of the PYPERSON.M implementation file are as follows
#import "PYPerson.h"
Import
@implementation Pyperson
(void) Encodewithcoder: (Nscoder) encoder
{
unsigned int count = 0;
Ivarivars = Class_copyivarlist ([Pyperson class], &count);
for (int i = 0; i
Remove the member variable corresponding to the I position Ivar Ivar = ivars[i];//View member Variable Constchar*name = Ivar_getname (Ivar);//Archive Nsstring*key = [ Nsstringstringwithutf8string:name];idvalue = [Selfvalueforkey:key]; [Encoder Encodeobject:value Forkey:key];
}
Free (ivars);
}
(ID) Initwithcoder: (Nscoder *) decoder
{
if (self = [super init]) {
Unsignedintcount =0;ivar *ivars = Class_copyivarlist ([Pyperson class], &count); for (Inti =0; i
}
return self;
}
@end
So we can see the case of the archive and the file is actually written by runtime.
Learning, the runtime mechanism must first understand the following questions
1 Related header files and functions
1> header File
With the header file, we can view the various methods in the runtime!
2> related applications
Nscoding (archive and file, use Runtime to traverse all properties of Model objects)
Dictionary –> model (use Runtime to traverse all properties of the model object, remove the corresponding value from the dictionary based on the property name, set to the properties of the model)
KVO (using runtime to generate a class dynamically)
For packaging frames (how to change them)
That's the way we use the runtime mechanism.
3> Correlation function
Objc_msgsend: Sending a message to an object
Class_copymethodlist: Traversing all methods of a class
Class_copyivarlist: Traversing all member variables of a class
Class_ .....
This is the function we must know to learn the runtime!
4. Essential Knowledge
1> Ivar: Member variable
2> method: Member Methods
From the example above we see the member variables we define, and if you are dynamically creating methods, you can use method
Runtime operating mechanism of OC