runtime 进行归档

作为iOS,runtime其实一定要去学习的,因为OC的代码最终都是转成runtime进行执行的。

github地址:https://github.com/wwpeter/WW-Runingtime.git

1.或许很多初学者很疑惑,什么是运行时(runtime)呢?

runtime是一套比较底层的纯C语言的API,runtime就是一个库,一个C语言库,包含了许多底层的C语言API
平时我们编写的OC代码,在程序运行过程中,其实最终都是转成了runtime的C语言代码,runtime算是OC的幕后工作者,是整个OC的底层
举个例子
oc中的代码:[Student alloc] init]经过runtime后,其实最终在底层生成C语言代码:
objc_msgSend(objc_msgSend("Student","alloc"), "init")
objc_msgSend其实就是向某个对象发送什么消息,这个函数第一个参数是类型或对象名,第二个参数是消息的名字,这些代码比较底层


2.runtime 的基础

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<objc/runtime.h> : //成员变量,类,方法
class_copyIvarList : 获得某个类内部的所有成员变量
class_copyMethodList : 获得某个类内部的所有方法
class_getInstanceMethod : 获得某个具体的实例方法 (对象方法,减号-开头)
class_getClassMethod : 获得某个具体的类方法 (加号+开头)
method_exchangeImplementations : 交换两个方法的实现
#import<objc/message.h> : //消息机制
objc_msgSend(...)

3.或许你想为这个鬼东西有是没用呢?

  • 能获得某个类的所有成员变量
  • 能获得某个类的所有属性
  • 能获得某个类的所有方法
  • 交换方法实现
  • 能动态添加一个成员变量
  • 能动态添加一个属性
  • 字典转模型
  • runtime归档/反归档

4、案例:下面我们用运行时获取成员变量/属性/私有,公开方法,并实现对Person实例的解归档

#import "SecondViewController.h"
#import "Person.h"
#import <objc/runtime.h>

@interface SecondViewController ()
{
    unsigned int couont;
}
@end

@implementation SecondViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    couont = 0;
    [self test5];
    //[self test1];
    //[self test4];
    [self test2];
    //[self test3];
}
-(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{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *pathStr = [path stringByAppendingPathComponent:@"person.plist"];
    
    Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:pathStr];
    NSLog(@"name = %@ score = %lf  arr = %@",p2.className,p2.score,p2.picUrls);
}
- (NSString *)description {
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *pathStr = [path stringByAppendingPathComponent:@"person.plist"];
    
    Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:pathStr];
    //NSLog(@"%@",p2);
    return [NSString stringWithFormat:@"%@",p2];
}
//归档
-(void)test5{
    Person *p = [[Person alloc] init];
    p.score = 155.8;
    p.className = @"wangwei";
    p.picUrls = @[@"1255553", @"4555556"];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *pathStr = [path stringByAppendingPathComponent:@"person.plist"];
    [NSKeyedArchiver archiveRootObject:p toFile:pathStr];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值