runtime是属于OC的底层,可以进行一些非常底层的操作(用OC无法实现的,或者说不好实现)eg
相关的头文件
// #import <objc/runtime.h>
// #import <objc/message.h>//消息发送机制,可以直接用底层函数,进行消息发送
// 相关函数
// msg_send:给对象发送消息,来自<objc/message.h>
// class_copyMethodList,遍历某个类中所有的方法,来自<objc/runtime.h>
//#pragma mark 实例变量方法是什么意思
// class_copyIvarList,遍历某个类中所有的实例变量的方法,来自<objc/runtime.h>
// 运行时必备常识:
// 1.Ivar:成员变量的意思
// 2.Method:成员方法的意思
// 3.property:属性
运行时归档,不用担心后期类增加属性,忘记添加归档
#import <Foundation/Foundation.h> @interface Person : NSObject<NSCoding> @property (nonatomic, strong) NSArray *picUrls; @property (nonatomic, copy) NSString *className; @property (nonatomic, assign) float score; @property (nonatomic, strong) NSNumber *number; - (void)demo; @end
Person.m文件
#import "Person.h" #import <objc/runtime.h> @implementation Person { int _age; double _height; NSString *_name; } - (void)test { NSLog(@"%s", __func__); } - (void)demo { NSLog(@"%s", __func__); } /* - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:self.picUrls forKey:@"picUrls"]; [encoder encodeObject:@(self.score) forKey:@"score"]; [encoder encodeObject:self.className forKey:@"className"]; } - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.picUrls = [decoder decodeObjectForKey:@"picUrls"]; self.score = [[decoder decodeObjectForKey:@"score"] doubleValue]; self.className = [decoder decodeObjectForKey:@"className"]; } return self; } */ -(void)encodeWithCoder:(NSCoder *)aCoder{ unsigned int count = 0; //1.取出所有的属性 objc_property_t *propertes = class_copyPropertyList([self class], &count); //2.遍历的属性 for (int i=0; i<count; i++) { //获取当前遍历的属性的名称 const char *propertyName = property_getName(propertes[i]); NSString *name = [NSString stringWithUTF8String:propertyName]; //利用KVC取出对应属性的值 id value = [self valueForKey:name]; //归档到文件中 [aCoder encodeObject:value forKey:name]; } } -(id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { unsigned int count =0; //1.取出所有的属性 objc_property_t *propertes = class_copyPropertyList([self class], &count); //2.遍历所有的属性 for (int i = 0; i < count; i++) { //获取当前遍历到的属性名称 const char *propertyName = property_getName(propertes[i]); NSString *name = [NSString stringWithUTF8String:propertyName]; //解归档前遍历得到的属性的值 id value = [aDecoder decodeObjectForKey:name]; // self.className = [decoder decodeObjectForKey:@"className"]; [self setValue:value forKey:name]; } } return self; } @end
延伸一下 runtime的使用
在控制器中我们可以测试用运行时获取 Person的实例变量,属性 ,私有和公开的方法
#import "ViewController.h" #import "Person.h" #import <objc/runtime.h> @interface ViewController () { unsigned int couont; } @end @implementation ViewController -(void)viewDidLoad{ [super viewDidLoad]; couont = 0; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //1.获取Person中所有的方法名称 [self test4]; } //1.获取Person中所有的方法名称 -(void)test1{ /** * 第一参数:需要获取的类 第二个参数:获取到的个数 */ Method *methds = class_copyMethodList([Person class], &couont); for (int i=0; i<couont; i++) { //1.获取遍历得到方法名 SEL m = method_getName(methds[i]); //2.将方法名称转换为字符串 NSString *methodName = NSStringFromSelector(m); //3.输出 NSLog(@"%@",methodName); } } //成员变量 -(void)test2{ Ivar *var = class_copyIvarList([Person class], &couont); for (int i = 0; i < couont; i++) { // 1.获取遍历到得成员变量名称 const char *varName = ivar_getName(var[i]); // 2.将变量名称转换为字符串 NSString *name = [NSString stringWithUTF8String:varName]; // 3.输出 NSLog(@"%@", name); } } //属性 -(void)test3{ objc_property_t *propertes = class_copyPropertyList([Person class], &couont); for (int i = 0; i < couont; i++) { // 1.获取遍历到得属性名称 const char * propertyName = property_getName(propertes[i]); // 2.将属性名称转换为字符串 NSString *name = [NSString stringWithUTF8String:propertyName]; // 3.输出 NSLog(@"%@", name); } } //解归档 -(void)test4{ Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/lanouhn/Desktop/Person.plist"]; NSLog(@"%@",p2); } //归档 -(void)test5{ Person *p = [[Person alloc] init]; p.score = 99.8; p.className = @"xxxx"; p.picUrls = @[@"123", @"456"]; [NSKeyedArchiver archiveRootObject:p toFile:@"/Users/lanouhn/Desktop/Person.plist"]; } @end