First involved in Runtime (1), first involved in runtime
Objective-C is a dynamic language, and many things are determined by runtime.
For example, the code first declares that testObject is an NSString, then creates an NSData Object, and saves the memory address of this Object in the test Object. When running, textObject only wants to be an NSData object.
NSString *testObject = [[NSData alloc]]init];
Runtime is a set of operating mechanisms. It is a set of relatively low-level APIs using C language, which contains many very good and powerful methods. The written OC code is converted to Runtime for implementation. Just like when we Initialize an object
OC: [[Person alloc] init] runtime: objc_msgSend ("Person", "alloc"), "init") when we call the method, OC: [object method] Runtime: object_msgSend (obj, @ selector (method ));
Since Runtime belongs to the bottom layer of OC, name can perform some underlying operations. What Objective-C can do is what it can do. What runtime can do is not necessarily what object-C can do. For example, we can add a class when the program is running and traverse the members of a class, you can also add an attribute for a class and modify an attribute value.
The following example uses Runtime to traverse model attributes and archive the attributes.
In the first step, we need to import the relevant header files. The header files during runtime are generally stored in the/objc file, which is commonly used <objc/runtime. h>, <objc/message>.
1. You can perform a test first. I wrote a piece of code in the viewDidLoad of the controller,
-(Void) viewDidLoad {[super viewDidLoad]; unsigned int count = 0; Ivar * ivars = class_copyIvarList ([SQKPerson class], & count); for (int I = 0; I <count; I ++) {// retrieve the member variable Ivar ivar = ivars [I] corresponding to the I position; // view the member variable const char * name = ivar_getName (ivar); NSLog (@ "% s", name );}
The printed result is
2015-10-28 22:47:41.186 runtime[2312:108141] _age2015-10-28 22:47:41.186 runtime[2312:108141] _height2015-10-28 22:47:41.186 runtime[2312:108141] _name
3. Create a Person class and declare three attributes, age, height, and name.
/// SQKPerson. m // runtime /// Created by SQK on 15/10/28. // Copyright©2015 SQK. all rights reserved. // # import "SQKPerson. h "# import <objc/runtime. h>/*** to archive the file, follow the NSCoding Protocol */@ interface SQKPerson () <NSCoding> // The attribute @ property (nonatomic, assign) is declared here) int age; @ property (nonatomic, assign) int height; @ property (nonatomic, copy) NSString * name; @ end @ implementation SQKPerson-(void) encodeWithCoder :( NSCoder *) encode {unsigned int count = 0; Ivar * ivars = class_copyIvarList ([SQKPerson class], & count); for (int I = 0; I <count; I ++) {// obtain the Ivars member variable Ivar ivar = ivars [I]; // you need to convert the name to a const char * name = ivar_getName (ivar) of the URTF8 type ); NSString * key = [NSString stringwithuf8string: name]; // archive id value = [self valueForKey: key]; [encode encodeObject: value forKey: key];} // memory needs to be managed free (ivars);}-(instancetype) initWithCoder :( NSCoder *) decoder {if (self = [super init]) {unsigned int count = 0; ivar * ivars = class_copyIvarList ([SQKPerson class], & count); for (int I = 0; I <count; I ++) {// retrieve the corresponding member variable Ivar ivar = ivars [I]; const char * name = ivar_getName (ivar); // end file NSString * key = [NSString stringwithuf8string: name]; // get the member variable (kvc) id value = [decoder decodeObjectForKey: key] through the key; // set the value to the member variable [self setValue: value forKey: key];} free (ivars);} return self;} @ end
If there are many attributes in our model, it is very convenient to use this method.
In short, runtime is a powerful set of language libraries. The official documents of Apple also provide detailed documents and explanations for various types
Class_getName
Class_addIvar
Class_copyIvarList
Class_getProperty
Class_getPropertyList
....................
In short, Runtime is a good set of things, and we hope we can all understand it. I write this thing while learning and writing it. It is inevitable to avoid incorrect opinions. Hope to make progress together with everyone!