NSKeyedArchiver项目实际应用

//已上传流量信息写入缓存

- (BOOL)saveUploadUsedFlowToCache:(CARespDayFlow*)uploadUsedFlow

{

NSString*cacheFilePath = [selfgetUsedFlowSavePath];

NSMutableDictionary*usedFlowDict = [NSMutableDictionarydictionaryWithDictionary:[NSKeyedUnarchiverunarchiveObjectWithFile:cacheFilePath]];

[usedFlowDictsetObject:uploadUsedFlowforKey:kDayFlowUsed_Upload];

return[NSKeyedArchiverarchiveRootObject:usedFlowDicttoFile:cacheFilePath];

}

- (NSString*)getUsedFlowSavePath

{

return[NSStringstringWithFormat:@"%@/UsedFlow.plist", [[VIPServiceCenterinstance]cacheDirectory]];

}

//从缓存加载已上传流量信息

- (CARespDayFlow*)getCacheUploadUsedFlow

{

//从缓存加载

NSMutableDictionary*usedFlowDict = [NSKeyedUnarchiverunarchiveObjectWithFile:[selfgetUsedFlowSavePath]];

CARespDayFlow*usedFlow = [usedFlowDictobjectForKey:kDayFlowUsed_Upload];

//判断拿到的记录是否是当天的

if(usedFlow && [[APIUtildateFromString:usedFlow.checkTime]isToday] ==NO)

{

returnnil;

}

returnusedFlow;

}

- (NSString*)cacheDirectory

{

NSString*dir = [NSStringstringWithFormat:@"%@/%@",

[UtilcurrentUserAppDataDirectory],

@"VIPServiceCache"];

if([[NSFileManagerdefaultManager]fileExistsAtPath:dir] ==NO)

{

[[NSFileManagerdefaultManager]createDirectoryAtPath:dirwithIntermediateDirectories:YESattributes:nilerror:nil];

}

returndir;

}

NSKeyedArchiver 处理缓存  plist方式,也可以用NSData

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容