美文网首页
Runtime之解归档

Runtime之解归档

作者: 倔强的小黄瓜 | 来源:发表于2016-08-16 15:04 被阅读11次

一、什么是运行时(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其实就是向某个对象发送什么消息,这个函数第一个参数是类型或对象名,第二个参数是消息的名字,这些代码比较底层
二、基础科普?

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:属性
三、运行时的作用?

能获得某个类的所有成员变量
能获得某个类的所有属性
能获得某个类的所有方法
交换方法实现
能动态添加一个成员变量
能动态添加一个属性
能动态添加一个方法
四、案例:下面我们用运行时获取成员变量/属性/私有,公开方法,并实现对Person实例的解归档

先自定义一个Person类 定义属性、实例变量和方法

文/风衫码农(简书作者)
原文链接:http://www.jianshu.com/p/78d226c102b1
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

#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


在控制器中我们可以测试用运行时获取 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

当然如上只是Runtime的凤毛麟角,更多详情还请大家参考官方API.

相关文章

  • Runtime之解归档

    一、什么是运行时(Runtime)? runtime是一套比较底层的纯C语言的API,runtime就是一个库,一...

  • iOS - runtime-04实现自动解归档

    通过 runtime 进行归档、解档很节省很多工作,我先贴一段常规的解归档的代码。 通过这种方式进行解归档很麻烦,...

  • runtime自动归档

    前言 善用runtime,可以解决自动归档解档。想想以前归档是手动写的,确实太麻烦了。现在有了runtime,我们...

  • runtime自动归档/解档

    前言 善用runtime,可以解决自动归档解档。想想以前归档是手动写的,确实太麻烦了。现在有了runtime,我们...

  • runTime

    iOS runtime讲解,并且用runtime动态归档与解档 (2015-09-30 22:42:02)转载▼ ...

  • runtime 进行归档和解档

    数据本地持久化时, 一般会将模型进行归档, 从本地获取数据时, 需要解档下面使用runtime的方式进行归档解档,...

  • runtime使用 自定义数据类型的编码解码

    通过runtime 遍历自定义model的所有属性实现归档解档操作。 要实现我们自定义的model能够自动解档归档...

  • objc/runtime之——归档反归档

    以上原始手动解档归档代码下载地址如下 runtime实现归档解档代码下载地址如下可以直接拉入项目使用 为什么我们要...

  • runtime 解档归档

  • 归档解档--runtime

    不说废话,直接上代码。首先创建一个Person类。Person.h import

网友评论

      本文标题:Runtime之解归档

      本文链接:https://www.haomeiwen.com/subject/tkupsttx.html