第一步肯定要是为本地缓存的文件夹取一个名字的,比如说叫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;
}
网友评论