zoukankan      html  css  js  c++  java
  • 在plist文件中增删改查

      plist文件是一种轻量级大数据存储方式,它的全名为PropertyList,即属性列表文件,它是一种用来存储串行化后的对象的文件。Plist文件是以key-value的方式来存取数据的。

         创建plist文件可通过xcode在工程中创建,也可通过代码来创建:

         1.plist文件的创建:

    NSFileManager *manager = [NSFileManager defaultManager];
        //找到沙盒路径下document所在的文件路径
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
        //创建user文件夹路径
        NSString *directorypath = [path stringByAppendingPathComponent:@"user"];
        //创建文件夹
        [manager createDirectoryAtPath:directorypath withIntermediateDirectories:false attributes:nil error:nil];
        //判断文件夹是否存在
        [self exictPath:directorypath];
        //删除文件夹
        [manager removeItemAtPath:directorypath error:nil];
        [self exictPath:directorypath];
        
        //在document路径下创建plist文件路径
       NSString *filepath = [path stringByAppendingPathComponent:@"moxue.plist"];
        //创建文件
        [manager createFileAtPath:filepath contents:nil attributes:nil];//如果需要修改文件的相关属性,可以通过NSFileManager得到文件的所有属性,然后设置相应的attributes值来更改,attributes值为字典类型
        //  NSLog(@"%@",[manager attributesOfItemAtPath:filepath error:nil]);
        [self exictPath:filepath];
        //移除文件
        [manager removeItemAtPath:filepath error:nil];
        [self exictPath:filepath];

           2.向plist文件添加数据

     //创建可变字典,在其中添加数据
     NSMutableDictionary *mainDict = [[NSMutableDictionary alloc]init];
        //在rootdict下创建一个子字典,并添加数据
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:@"liu" forKey:@"one"];
        [dict setObject:@"zhi" forKey:@"two"];
        //将创建的子字典添加到向plist传输数据的字典
        [mainDict setValue:dict forKey:@"item1"];
        //通过mainDict向plist写入数据
        [mainDict writeToFile:filepath atomically:YES];
        //插入第二条子字典
        NSMutableDictionary *dict11 = [[NSMutableDictionary alloc]init];
        [dict11 setObject:@"liu" forKey:@"one"];
        [dict11 setObject:@"zhi" forKey:@"two"];
        [mainDict setValue:dict11 forKey:@"item2"];
        [mainDict writeToFile:filepath atomically:YES];

          3.在plist文件中删除数据

    //在plist列表中移除第二个字典
        [mainDict removeObjectForKey:@"item2"];
        [mainDict writeToFile:filepath atomically:YES];

         4.查看plist文件中的数据

    //得到plist列表中所有的数据并打印
        NSDictionary *lookdict = [NSDictionary dictionaryWithContentsOfFile:filepath];
        NSLog(@"lookdict == %@",lookdict);

     

          

  • 相关阅读:
    sqlserver 动态 sql语句的执行
    SqlServer位运算 权限设计
    更改主数据 的管理员账户
    如何查看dll 的PublicKeyToken
    varbinary 与 字符串 的互换函数
    analysis service 配置远程连接
    sqlserver字符串拆分(split)方法汇总
    openfile 安装备忘
    Lamp 在centos 中的安装
    Oracle查询表中的各列的列名,数据类型,以及类型长度
  • 原文地址:https://www.cnblogs.com/moxuexiaotong/p/4918938.html
Copyright © 2011-2022 走看看