First, we first understand the runtime implementation mechanism and role
1. What is the mechanism of runtime implementation?
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")
2. What is runtime used for?? Where do you use them?
Runtime is the bottom of the OC, you can do some very low-level operations (with OC things can not be achieved, not good implementation)
(1) Dynamically create a class during program run (for example, KVO implementation)
(2) Dynamically add properties \ Methods to a class, modify property values \ Methods during program run
(3) Traverse all member variables (attributes) 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!
@interface Pyperson: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
<!-- lang: cpp -->#import "PYPerson.h"
Import
@implementation Pyperson
(void) Encodewithcoder: (Nscoder ) encoder
{
unsigned int count = 0;
Ivar ivars = Class_copyivarlist ([Pyperson class], &count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量Ivar ivar = ivars[i];// 查看成员变量const char *name = ivar_getName(ivar);// 归档NSString *key = [NSString stringWithUTF8String:name];id value = [self valueForKey:key];[encoder encodeObject:value forKey:key];
}
Free (ivars);
}
(ID) Initwithcoder: (Nscoder *) decoder
{
if (self = [super init]) {
unsigned int count = Span class= "Hljs-number" >0;ivar *ivars = Class_copyivarlist ([Pyperson class], &count); for (int i = 0; i<count; i++) { Span class= "hljs-comment" >//remove the member variable corresponding to the i position Ivar Ivar = ivars[i]; //View member variables const char *name = Ivar_ GetName (Ivar); //archive nsstring *key = [NSString Stringwithutf8string:name]; id value = [Decoder decodeobjectforkey:key]; //set to member variable body [self setvalue:value Forkey:key];} Free (ivars);
}
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
NSString *nsstringfromselector (SEL aselector); method name to String
SEL nsselectorfromstring (nsstring *aselectorname); string Goto method Name
nsstring *nsstringfromclass (class AClass); class name to String
Class nsclassfromstring (nsstring *aclassname); string goto class name
nsstring *nsstringfromprotocol (Protocol *proto); protocol name to String
Protocol *nsprotocolfromstring (nsstring *namestr); string to protocol name
- 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 that we define, and if you are dynamically creating methods, you can use method,
Runtime mechanism principle and application