美文网首页iOS开发ios实用开发技巧
plist文件代码创建,读取,删除

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

作者: 不要动 | 来源:发表于2016-11-14 10:33 被阅读90次

    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];

    相关文章

      网友评论

        本文标题:plist文件代码创建,读取,删除

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