plist文件代码创建,读取,删除

1,创建

//获取路径对象

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//获取完整路径

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"下载成功.plist"];

NSMutableArray *list  = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];

NSMutableArray * list1;

if (list == nil)

{

list1  = [[NSMutableArray alloc]init];

}

else

{

list1=[[NSMutableArray alloc]initWithContentsOfFile:plistPath];

}

NSMutableDictionary *allList = [[NSMutableDictionary alloc] init];

[allList setValue:[NSString stringWithFormat:@"%@",model.name] forKey:@"ListName"];

[allList setValue:[NSString stringWithFormat:@"%@",model.destinationPath] forKey:@"ListFilePath"];

[allList setValue:[NSString stringWithFormat:@"%@",nameimage] forKey:@"ListFilePathimage"];

[list1 addObject:allList];

[list1 writeToFile:plistPath atomically:YES];

2,读取


NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSString *dictFilePath = [path stringByAppendingPathComponent:@"下载成功.plist"];

Patharra  =[NSMutableArray arrayWithContentsOfFile:dictFilePath];

3删除


NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *path=[paths    objectAtIndex:0];

NSString *filename=[path stringByAppendingPathComponent:@"下载成功.plist"];

NSMutableArray * muarraybn =[[NSMutableArray alloc]initWithContentsOfFile:filename];


3.1 删除整个plist文件

NSFileManager *fileManager = [NSFileManager defaultManager];

[fileManager removeItemAtPath:filename error:nil];

3.2 删除文件里面的某个值

    [muarraybn removeObjectAtIndex:indexPath.row];

    [muarraybn writeToFile:filename atomically:YES];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容