本文主要是介绍ios 往 Library的cache目录下 存放缓存数据的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 写缓存
+ (void)writeLocalCacheData:(NSData *)data withKey:(NSString *)key {// 设置存储路径NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]stringByAppendingPathComponent:key];// 判读缓存数据是否存在if ([[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {// 删除旧的缓存数据[[NSFileManager defaultManager] removeItemAtPath:cachesPath error:nil];}// 存储新的缓存数据[data writeToFile:cachesPath atomically:YES];
}// 读缓存
+ (NSData *)readLocalCacheDataWithKey:(NSString *)key {NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]stringByAppendingPathComponent:key];// 判读缓存数据是否存在if ([[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {// 读取缓存数据return [NSData dataWithContentsOfFile:cachesPath];}return nil;
}// 删缓存
+ (void)deleteLocalCacheDataWithKey:(NSString *)key {NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]stringByAppendingPathComponent:key];// 判读缓存数据是否存在if ([[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {// 删除缓存数据[[NSFileManager defaultManager] removeItemAtPath:cachesPath error:nil];}
}
这篇关于ios 往 Library的cache目录下 存放缓存数据的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!