一:归档与解档
(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;
}
+(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;
}