数据存储方式IOS---归档与解档

本文介绍了一种iOS应用中实现数据归档与解档的方法。通过将模型类的数据转换为字典形式并利用NSKeyedArchiver进行归档,然后将归档数据存储在本地缓存中。在需要使用时,再从缓存读取数据进行解档,并重建模型类。此方法适用于需要长期存储数据的应用场景。

一:归档与解档

(1::::::)归档

1:保存数据到模型类,如下:

- (instancetype)initWithDic:(NSDictionary*)dic

{

    self = [super init];

    if (self) {

        //

        

        //转模型

        for (NSString* key in [self getAllPropertys]) {

            //

            NSArray* tmparr = [key componentsSeparatedByString:@"p_"];

            NSString* tmpkey =tmparr.lastObject;

            

            id propertyValue = [dic valueForKey:tmpkey];

            [self setValue:[LSFactory fc_judgeObj:propertyValue placeholder:@""] forKey:key];

        }

        

        //

    }

    return self;

}


2:将该模型类归档

+(void)fc_setValueModel:(YHBaseModel *)model ForKey:(NSString *)key{

    NSMutableData * data = [[NSMutableData alloc]init];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    [archiver encodeObject:model forKey:key];

    [archiver finishEncoding];

    [self fc_writeCecheFile:data withFileID:key];

}


3:将归档后的数据写进本地缓存

+(void)fc_writeCecheFile:(NSData *)data

           withFileID:(NSString *)fileID{

    NSString* path = [NSHomeDirectory() stringByAppendingString:kLS_BASE_CECHE_PATH_Archiver];

    NSFileManager * fileManager = [NSFileManager defaultManager];

    //判断目录是否存在

    if (![fileManager fileExistsAtPath:cechePath]) {

        //创建目录

        [fileManager createDirectoryAtPath:cechePath withIntermediateDirectories:YES attributes:nil error:nil];

    }

    //拼接文件路径

    NSMutableString * tempStr = [[NSMutableString alloc]init];

    for (int i=0; i<fileID.length; i++) {

        if (([fileID characterAtIndex:i]<='9'&&[fileID characterAtIndex:i]>='0')||([fileID characterAtIndex:i]<='z'&&[fileID characterAtIndex:i]>='a')||([fileID characterAtIndex:i]<='Z'&&[fileID characterAtIndex:i]>='A')) {

            [tempStr appendFormat:@"%c",[fileID characterAtIndex:i]];

        }

    }

    NSString * writePath = [cechePath stringByAppendingFormat:@"/%@",tempStr];

    [data writeToFile:writePath atomically:YES];

    //    NSLog(@"写缓存");

}






#pra mark -获取说有的成员变量

-(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;

}




2:(接档)

1.根据路径先找data

+(NSData *)fc_readCecheFile:(NSString *)fileID fromPath:(NSString*)cechePath{

    NSMutableString * tempStr = [[NSMutableString alloc]init];

    for (int i=0; i<fileID.length; i++) {

        if (([fileID characterAtIndex:i]<='9'&&[fileID characterAtIndex:i]>='0')||([fileID characterAtIndex:i]<='z'&&[fileID characterAtIndex:i]>='a')||([fileID characterAtIndex:i]<='Z'&&[fileID characterAtIndex:i]>='A')) {

            [tempStr appendFormat:@"%c",[fileID characterAtIndex:i]];

        }

    }

    NSString * readPath = [cechePath stringByAppendingFormat:@"/%@",tempStr];

    NSFileManager * fileManager = [NSFileManager defaultManager];

    //判断文件是否

    if (![fileManager fileExistsAtPath:readPath]) {

        return nil;

    }else{

        NSData *data = [NSData dataWithContentsOfFile:readPath];

        return data;

    }

}

2.找到data 之后开始接档,创建数据模型类

+(YHBaseModel *)fc_getValueModel:(NSString *)key{

    NSString* path = [NSHomeDirectory() stringByAppendingString:kLS_BASE_CECHE_PATH_Archiver];

    NSData * data = [self fc_readCecheFile:key fromPath:path];

    NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

    YHBaseModel * model = [unArchiver decodeObjectForKey:key];

    [unArchiver finishDecoding];

    return model;

}




计及源荷不确定性的综合能源生产单元运行调度容量配置优化研究(Matlab代码实现)内容概要:本文围绕“计及源荷不确定性的综合能源生产单元运行调度容量配置优化”展开研究,利用Matlab代码实现相关模型的构建仿真。研究重点在于综合能源系统中多能耦合特性以及风、光等可再生能源出力和负荷需求的不确定性,通过鲁棒优化、场景生成(如Copula方法)、两阶段优化等手段,实现对能源生产单元的运行调度容量配置的协同优化,旨在提高系统经济性、可靠性和可再生能源消纳能力。文中提及多种优化算法(如BFO、CPO、PSO等)在调度预测中的应用,并强调了模型在实际能源系统规划运行中的参考价值。; 适合人群:具备一定电力系统、能源系统或优化理论基础的研究生、科研人员及工程技术人员,熟悉Matlab编程和基本优化工具(如Yalmip)。; 使用场景及目标:①用于学习和复现综合能源系统中考虑不确定性的优化调度容量配置方法;②为含高比例可再生能源的微电网、区域能源系统规划设计提供模型参考和技术支持;③开展学术研究,如撰写论文、课题申报时的技术方案借鉴。; 阅读建议:建议结合文中提到的Matlab代码和网盘资料,先理基础模型(如功率平衡、设备模型),再逐步深入不确定性建模优化求过程,注意区分鲁棒优化、随机优化分布鲁棒优化的适用场景,并尝试复现关键案例以加深理
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值