美文网首页
通过RunTime设置可自动归档的数据模型基类

通过RunTime设置可自动归档的数据模型基类

作者: 流云_henry | 来源:发表于2020-07-21 11:37 被阅读0次

在iOS开发中,归档是一种很常用的数据持久化方法。可以对于归档,我们必须要实现NSCoding中的两个方法,十分的繁琐。
这里我们通过运行时来进行修改,使需要归档的模型类都继承自它,使我们的数据模型不需要实现NSCoding方法,同时支持归档。示例如下:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface BaseModel : NSObject <NSCoding>
/**
 保存
 */
- (void)saveModel;

/**
 删除
 */
+ (void)removeModel;
@end

NS_ASSUME_NONNULL_END




#import "BaseModel.h"
#import <objc/runtime.h>
@implementation BaseModel 
//归档与解档的方法
- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        //获取所有属性
        NSArray *porpertyArray = [self getAllPropertys];
        for (NSString *name in porpertyArray) {
            //去掉属性名签名的_
            NSString *key = [name substringFromIndex:1];
            //约定好的键值对 c+key
            [self setValue:[coder decodeObjectForKey:[NSString stringWithFormat:@"c%@",key]] forKey:key];
            
        }
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    //获取所有属性
    NSArray *porpertyArray = [self getAllPropertys];
    for (NSString *name in porpertyArray) {
        //去掉属性名签名的_
        NSString *key = [name substringFromIndex:1];
        //约定好的键值对 c+key
        [coder encodeObject:[self valueForKey:key] forKey:[NSString stringWithFormat:@"c%@",key]];
    }
}

//获取model所有的属性
- (NSArray *)getAllPropertys {
    NSMutableArray *array = [[NSMutableArray alloc]init];
    unsigned int *count = malloc(sizeof(unsigned int));
    //调用runtime方法
    //Ivar:方法返回的对象,这里将放回一个Ivar类型的指针
    //用class_copyIvarList方法可以捕获到类的所有变量,将变量的数量存在一个unsigned int的指针中
    Ivar *mem = class_copyIvarList([self class], count);
    //遍历
    for (int i = 0; i < *count; i ++) {
        //通过移动指针进行遍历
        Ivar var = *(mem + i);
        //获取变量名
        const char *name = ivar_getName(var);
        NSString *str = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
        [array addObject:str];
    }
    //释放内存
    free(count);
    //注意处理野指针
    count = nil;
    return array;
}

/**
 保存
 */
- (void)saveModel{
    NSString *savePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.data",NSStringFromClass([self class])]];
    [NSKeyedArchiver archiveRootObject:self toFile:savePath];
}

/**
 删除
 */
+ (void)removeModel {
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSString *savePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.data",NSStringFromClass([self class])]];
    [fileManage removeItemAtPath:savePath error:nil];
}

@end

通过这样的一个运行时机制,我们可以很方便地使用新建的数据模型类继承自这个基类,无需其他处理直接归档,修改与优化都不受影响。

相关文章

  • 通过RunTime设置可自动归档的数据模型基类

    在iOS开发中,归档是一种很常用的数据持久化方法。可以对于归档,我们必须要实现NSCoding中的两个方法,十分的...

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

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

  • runtime自动归档

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

  • runtime自动归档/解档

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

  • 归档与设计可存储化的数据模型基类

    NSString *homeDictionary = NSHomeDirectory();NSString *ho...

  • runtime 自动归档

    提到归档这块,首先得看了一下,常规的归档方法(又名序列化),把对象转为字节码,以文件的形式存储到磁盘上;程序运行过...

  • 归档

    1、什么叫归档归档:即序列化。任何对象都可以遵循协议进行归档。通过对数据模型对象进行归档可以轻...

  • NSCoding的自动归档和自动解档的正确姿势

    有时候我们需要归档的时候,对于自定义类需要手写自动归档接档,当属性名比较多的时候一个个写容易出错,用runtime...

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

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

  • 利用runtime归档-解档

    利用runtime归档的好处就在于,不管类的属性有多少个,都不用担心,它都会自动帮你处理好。 首先新建一个Pers...

网友评论

      本文标题:通过RunTime设置可自动归档的数据模型基类

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