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

你可能感兴趣的:(NSKeyedArchiver项目实际应用)