一:归档与解档
(1::::::)归档
1:保存数据到模型类,如下:
- (instancetype)initWithDic:(NSDictionary*)dic
{
self = [superinit];
if (self) {
//
//转模型
for (NSString* keyin [selfgetAllPRopertys]) {
//
NSArray* tmparr = [keycomponentsSeparatedByString:@"p_"];
NSString* tmpkey =tmparr.lastObject;
id propertyValue = [dicvalueForKey:tmpkey];
[selfsetValue:[LSFactoryfc_judgeObj:propertyValueplaceholder:@""]forKey:key];
}
//
}
returnself;
}
2:将该模型类归档+(void)fc_setValueModel:(YHBaseModel *)model ForKey:(NSString *)key{
NSMutableData * data = [[NSMutableDataalloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
[archiver encodeObject:modelforKey:key];
[archiver finishEncoding];
[selffc_writeCecheFile:data withFileID:key];
}
3:将归档后的数据写进本地缓存
+(void)fc_writeCecheFile:(NSData *)data
withFileID:(NSString *)fileID{
NSString* path = [NSHomeDirectory()stringByAppendingString:kLS_BASE_CECHE_PATH_Archiver];
NSFileManager * fileManager = [NSFileManagerdefaultManager];
//判断目录是否存在
if (![fileManagerfileExistsAtPath:cechePath]) {
//创建目录
[fileManager createDirectoryAtPath:cechePathwithIntermediateDirectories:YESattributes:nilerror:nil];
}
//拼接文件路径
NSMutableString * tempStr = [[NSMutableStringalloc]init];
for (int i=0; i<fileID.length; i++) {
if (([fileIDcharacterAtIndex:i]<='9'&&[fileIDcharacterAtIndex:i]>='0')||([fileIDcharacterAtIndex:i]<='z'&&[fileIDcharacterAtIndex:i]>='a')||([fileIDcharacterAtIndex:i]<='Z'&&[fileIDcharacterAtIndex:i]>='A')) {
[tempStr appendFormat:@"%c",[fileIDcharacterAtIndex:i]];
}
}
NSString * writePath = [cechePathstringByAppendingFormat:@"/%@",tempStr];
[data writeToFile:writePathatomically:YES];
// NSLog(@"写缓存");
}
#pra mark -获取说有的成员变量
-(NSArray *)getAllPropertys{
NSMutableArray * array = [[NSMutableArrayalloc]init];
unsignedint * count = malloc(sizeof(unsignedint));
//调用runtime的方法
//Ivar:方法返回的对象内容对象,这里将返回一个Ivar类型的指针
//class_copyIvarList方法可以捕获到类的所有变量,将变量的数量存在一个unsigned int的指针中
Ivar * mem =class_copyIvarList([selfclass], count);
//进行遍历
for (int i=0; i< *count ; i++) {
//通过移动指针进行遍历
Ivar var = * (mem+i);
//获取变量的名称
constchar * name = ivar_getName(var);
NSString * str = [NSStringstringWithCString: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 = [[NSMutableStringalloc]init];
for (int i=0; i<fileID.length; i++) {
if (([fileIDcharacterAtIndex:i]<='9'&&[fileIDcharacterAtIndex:i]>='0')||([fileIDcharacterAtIndex:i]<='z'&&[fileIDcharacterAtIndex:i]>='a')||([fileIDcharacterAtIndex:i]<='Z'&&[fileIDcharacterAtIndex:i]>='A')) {
[tempStr appendFormat:@"%c",[fileIDcharacterAtIndex:i]];
}
}
NSString * readPath = [cechePathstringByAppendingFormat:@"/%@",tempStr];
NSFileManager * fileManager = [NSFileManagerdefaultManager];
//判断文件是否
if (![fileManagerfileExistsAtPath:readPath]) {
returnnil;
}else{
NSData *data = [NSDatadataWithContentsOfFile:readPath];
return data;
}
}
2.找到data 之后开始接档,创建数据模型类
+(YHBaseModel *)fc_getValueModel:(NSString *)key{
NSString* path = [NSHomeDirectory()stringByAppendingString:kLS_BASE_CECHE_PATH_Archiver];
NSData * data = [selffc_readCecheFile:key fromPath:path];
NSKeyedUnarchiver * unArchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:data];
YHBaseModel * model = [unArchiverdecodeObjectForKey:key];
[unArchiver finishDecoding];
return model;
}
新闻热点
疑难解答