plist,全名PropertyList,即属性列表文件,它是一种用来存储串行化后的对象的文件。这种文件,在ios开发过程中经常被用到。这种属性列表文件的扩展名为.plist,因此通常被叫做plist文件。文件是xml格式的。Plist文件是以key-value的形式来存储数据。既可以用来存储用户设置,也可以用来存储一些需要经常用到而不经常改动的信息。
在对plist文件的操作有创建,删除,写入和读取。这四种操作中,写入和读取是比较常用的操作。
我第一次用是缓存登录信息 为了实现一键登录 免得每次都得输入账号和密码
我把整个操作plist过程封装成了类方法,因为每次都得读取路径 我以为会很慢,打算放入线程,可能是数据规模小,我就没放进线程。而且速度还是很快。
以下是类方法
@interface Function : NSObject +(NSString *)CreateTheFolder_From:(NSString *)fileStyle FileHolderName:(NSString *)folderName FileName:(NSString *)fileName;//有文件夹则读取路径 无则创建之后再读取路径 并返回路径 +(NSString *)achieveThe_filepath:(NSString *)fileName Kind:(NSString *)fileStyle;//获取文件路径 或者文件夹路径 fileName 为文件夹名称或者带后缀的文件比如1.png +(BOOL)judgeFileExist:(NSString * )fileName Kind:(NSString *)fileStyle;//判断文件是否存在 YES 存在 NO不存在 +(void)creatTheFile:(NSString *)fileName Kind:(NSString *)fileStyle;//创建一个名为fileName的文件 +(void)WriteToFile:(NSString *)fileName File:(NSDictionary *)dic Kind:(NSString *)fileSyle;//把file 写入名为fileName的文件 +(NSDictionary*)ReadFromFile:(NSString *)fileName Kind:(NSString *)fileStyle;//从名为fileName的文件 读取 数据内容 +(void)DeleteTheFile:(NSString *)fileName Kind:(NSString *)fileSyle;//删除文件操作 +(void)Revision_TheFile_Name:(NSString *)fileName File:(NSString *)txt Key:(NSString *)key Kind:(NSString *)fileStyle;//修改文件中某条信息记录 +(void)Delete_TotalFileFromPath:(NSString *)fileAndFolder_Path ;//删除路径下所有缓存文件包括文件夹自己 @end //类方法的实施 #import "Function.h" @implementation Function+(NSString*)CreateTheFolder_From:(NSString *)fileStyle FileHolderName:(NSString *)folderName FileName:(NSString *)fileName { //创建文件夹: //查找路径的时候 会创建该文件夹 所以即使做删除所有文件包括文件夹后 最后查找路径的时候还是会被重新创建,这个路径就是该应用的归属 不错不是吗 NSString *filePath=[Function achieveThe_filepath:nil Kind:fileStyle]; filePath = [filePath stringByAppendingPathComponent:folderName]; BOOL isDir = NO; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir]; if ( !(isDir == YES && existed == YES) ) { [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; } filePath = [filePath stringByAppendingPathComponent:fileName]; return filePath; } +(NSString *)achieveThe_filepath:(NSString *)fileName Kind:(NSString *)fileSyle { NSString *filePath; if([fileSyle isEqual:Document]) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //获取完整路径 NSString *documentsDirectory = [paths objectAtIndex:0]; filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; } else if([fileSyle isEqual:Library_Cache]) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); //获取完整路径 NSString *documentsDirectory = [paths objectAtIndex:0]; filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; } else//tmp { filePath=[NSString stringWithFormat:@"%@%@",NSTemporaryDirectory(),fileName]; } return filePath; } +(BOOL)judgeFileExist:(NSString * )fileName Kind:(NSString *)fileStyle { NSString *filepath=[Function CreateTheFolder_From:fileStyle FileHolderName:MyFolder FileName:fileName]; NSFileManager *fileManager = [NSFileManager defaultManager]; return [fileManager fileExistsAtPath:filepath]; //返回 YES 存在 NO不存在 } +(void)creatTheFile:(NSString *)fileName Kind:(NSString *)fileStyle { NSString *filepath=[Function CreateTheFolder_From:fileStyle FileHolderName:MyFolder FileName:fileName]; NSFileManager* fm = [NSFileManager defaultManager]; [fm createFileAtPath:filepath contents:nil attributes:nil]; } +(void)WriteToFile:(NSString *)fileName File:(NSDictionary *)dic Kind:(NSString *)fileStyle { NSString *filepath=[Function CreateTheFolder_From:fileStyle FileHolderName:MyFolder FileName:fileName]; [dic writeToFile:filepath atomically:YES]; } +(NSDictionary *)ReadFromFile:(NSString *)fileName Kind:(NSString *)fileStyle { NSString *filepath=[Function CreateTheFolder_From:fileStyle FileHolderName:MyFolder FileName:fileName]; NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:filepath]; return dic; } +(void)DeleteTheFile:(NSString *)fileName Kind:(NSString *)fileSyle { NSString *filepath=[Function achieveThe_filepath:fileName Kind:fileSyle]; //删除文件 NSFileManager *fm = [NSFileManager defaultManager]; [fm removeItemAtPath:filepath error:nil]; if([Function judgeFileExist:fileName Kind:fileSyle]) { NSLog(@"删除失败"); } else { NSLog(@"删除成功"); } } +(void)Revision_TheFile_Name:(NSString *)fileName File:(NSString *)txt Key:(NSString *)key Kind:(NSString *)fileSyle { NSString *filepath=[Function achieveThe_filepath:fileName Kind:fileSyle]; NSMutableDictionary *applist = [[NSMutableDictionary alloc] initWithContentsOfFile:filepath]; // 修改一个plist文件数据 [applist setObject:txt forKey:key]; //修改后直接把数据再写入文件 [applist writeToFile:filepath atomically:YES]; } +(void)Delete_TotalFileFromPath:(NSString *)fileAndFolder_Path { [[NSFileManager defaultManager] removeItemAtPath:fileAndFolder_Path error:nil]; } @end
具体的使用举例
//如果当前文件不存在 则创建文件 并且插入数据 //如果存在 则更改表数据 并 读取文件内容 //功能都是封装好 独立的 也可以删除 等
...... #import "Function.h" #define login @ "login.plist" #define Document @ "document" #define Library_Cache @ "library_cache" #define Tmp @ "tmp" ...... //比如以缓存登录账号和密码为例,每次打开应用判断是否登录,如果登录过 从缓存中读取账号和密码并显示。 如方法: //上次是否登录过 -(void)isLoad_past { if([Function judgeFileExist:login Kind:Library_Cache]) { NSDictionary* dic =[Function ReadFromFile:login Kind:Library_Cache]; textField_Account.text=[dic objectForKey:@"account"]; textField_Secret.text=[dic objectForKey:@"secret"]; } } /* 点击登录后 判断账号密码是否存在 存在 则更新 不存在 创建文件 写入文件 方法如下 */ if([Function judgeFileExist:login Kind:Library_Cache]) { // [Function DeleteTheFile:login]; [Function Revision_TheFile_Name:login File:textField_Account.text Key:@"account" Kind:Library_Cache]; [Function Revision_TheFile_Name:login File:textField_Secret.text Key:@"secret" Kind:Library_Cache]; } else { NSMutableDictionary *dictplist = [[NSMutableDictionary alloc ] init]; //设置属性值 [dictplist setObject:textField_Account.text forKey:@"account"]; [dictplist setObject:textField_Secret.text forKey:@"secret"]; NSDictionary *dict=dictplist; [Function creatTheFile:login Kind:Library_Cache]; [Function WriteToFile:login File:dict Kind:Library_Cache]; }
/*
做清理缓存操作时候 删除文件夹 及里面的所有文件
做清除缓存的删除操作的集合方法举例
NSString *str1= [Function achieveThe_filepath:MyFolder Kind:Library_Cache];
[Function Delete_TotalFileFromPath:str1];
*/