美文网首页
用plist方式实现本地缓存

用plist方式实现本地缓存

作者: 邓布利多教授 | 来源:发表于2019-03-02 13:14 被阅读0次

第一步肯定要是为本地缓存的文件夹取一个名字的,比如说叫xiaohua

创建一个类,比如类名叫:LYCachePlistManager

//获取文件路径
+(NSString *)path:(NSString *)pathName{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathIndes = [array objectAtIndex:0];
    NSString *path = [pathIndes stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",pathName]];
    return path;
}

创建一个叫做xiaohua的plist文件

[LYCachePlistManager pathName:xiaohua];

写入文件

NSMutableDictionary *plistDict = [[NSMutableDictionary alloc]init];
 //添加新数据
[plistDict setObject:@“obj” forKey:@“key”];
//写入plist里面
 [plistDict writeToFile:[LYCachePlistManager path:xiaohua] atomically:YES];

删除文件

NSFileManager *fileMger = [NSFileManager defaultManager];
//要先判断文件路径是否存在
BOOL isBool = [fileMger fileExistsAtPath:[LYCachePlistManager path:xiaohua]];
if (isBool) {              
    [fileMger removeItemAtPath:xiaoXiPath error:nil];    
 }

获取plist文件

//获取plist文件字典
NSDictionaty *dict = [LYCachePlistManager pathName:xiaohua];
NSLog(@“dict = %@”, dict);

+(NSMutableDictionary *)pathName:(NSString *)pathName{
    NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[LYCachePlistManager path:pathName]];
    return dataDictionary;
}

相关文章

  • 用plist方式实现本地缓存

    第一步肯定要是为本地缓存的文件夹取一个名字的,比如说叫xiaohua 创建一个叫做xiaohua的plist文件 ...

  • iOS本地五种缓存数据方式

    前言 iOS本地缓存数据方式有五种: 1.直接写文件方式*.plist:可以存储的对象有NSString、NSAr...

  • 自己实现缓存

    LRU缓存实现(Java)java实现本地缓存

  • Guava cache使用总结

    缓存分为本地缓存和远端缓存。常见的远端缓存有Redis,MongoDB;本地缓存一般使用map的方式保存在本地内存...

  • iOS数据持久化

    本地数据存储有哪几种方式?如何实现复杂对象的存储? plist文件 属性表(property list)是Coco...

  • Guava Cache 使用

    缓存分为本地缓存和远端缓存。常见的远端缓存有 Redis,MongoDB;本地缓存一般使用 map 的方式保存在本...

  • 聊聊缓存

    本地缓存:mybatis实现:装饰器模式实践 PerpetualCache:永久缓存:通过HashMap实现最大容...

  • iOS中plist文件

    plist文件 plist文件储存本地数据的一种方式 json实例对象,字典或者数组 跟目录 写入数据到plist...

  • iOS开发之性能优化<一>

    一、加载本地图片是否缓存 1.常见的加载本地图片有两种方式: 1.用imageNamed加载2.用imageWit...

  • 本地plist文件实现瀑布流照片墙

    本地plist文件实现瀑布流照片墙 @property (nonatomic,strong) NSMutableA...

网友评论

      本文标题:用plist方式实现本地缓存

      本文链接:https://www.haomeiwen.com/subject/indeuqtx.html