现在的位置: 首页 > 综合 > 正文

IOS 对plist文件的读写

2017年10月30日 ⁄ 综合 ⁄ 共 4040字 ⁄ 字号 评论关闭

在做iOS开发时,经常用到到plist文件,  那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。

Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息


我们创建一个项目来学习plist文件的读写。

1、首先新建一个项目LTReadWritePlist,项目创建好以后,系统会默认创建一个plist文件,
     我们创建的工程中,plist文件是在Supporting Files下面的LTReadWritePlist-Info.plist,打开显示如下:


在编辑器中显示的形式与表格类似,另外也可以使用Source code形式打开,打开plist是xml格式的。

2、创建plist文件,

  选中工程中的Supporting Files,右键点击,选择Add File to "工程名",
  在iOS下面的Resource中选择Property List,创建一个school.plist的文件,
  打开新创建的文件,显示如下:


点击Key 下面的Root后面的加号,添加一个数据,同时修改Type为Dictionary,然后在下面添加数据,添加完成以后数据显示如下图:


3、读取plist文件数据

读取代码如下:

 /*对plist文件的读*/
    NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"schoolinfo" ofType:@"plist"];
    NSMutableDictionary *data=[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"%@",data);
    /*读取数据结束*/

显示如下:


想schoolinfo.plist中追加数据,代码如下:

//在plist中追加写入数据
    [data setObject:@"20" forKey:@"count"];
    [data writeToFile:plistPath atomically:YES];//保存

追加数据以后显示如下:


4、创建与写plist文件

代码如下(data数据接上):

 /*保存数据一*/
    /*新建一个plist*/
    //获取应用程序沙盒的Documents目录
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *newplistPath = [paths objectAtIndex:0];
    //得到完整的文件名
    NSString *filename=[newplistPath stringByAppendingPathComponent:@"task.plist"];
    //保存数据
    [data writeToFile:filename atomically:YES];
    /*保存数据一结束*/
    
    //将保存的数据读出来
    NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", data1);

5、创建string类型plist

代码如下:

/*写入string数据*/
    //获取路径对象
    NSArray *path1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentpath=[path1 objectAtIndex:0];
    NSString *stringplistpath=[documentpath stringByAppendingPathComponent:@"stringinfo.plist"];
    NSMutableDictionary *dicplist=[NSMutableDictionary dictionary];
    //设置属性
    [dicplist setObject:@"20" forKey:@"age"];
    [dicplist setObject:@"male" forKey:@"sex"];
    [dicplist setObject:@"sports" forKey:@"hobby"];
    //写入文件
    [dicplist writeToFile:stringplistpath atomically:YES];
    /*结束写入string数据*/

6、创建Dictionary类型plist

    /*创建并写入Dictionary键值plist*/
    NSArray *path2=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentpath2=[path2 objectAtIndex:0];
    NSString *dicplistpath=[documentpath2 stringByAppendingPathComponent:@"dicinfo.plist"];
    
    NSMutableDictionary *rootdicplist=[NSMutableDictionary dictionary];
    
    //定义第一个Dictionary集合
    NSMutableDictionary *child1plist=[NSMutableDictionary dictionary];
    [child1plist setObject:@"25" forKey:@"age"];
    [child1plist setObject:@"female" forKey:@"sex"];
    [child1plist setObject:@"basketball" forKey:@"hobby"];
    
    //添加到根集合中
    [rootdicplist setObject:child1plist forKey:@"xiaohua"];
    
    //定义第二个Dictionary集合
    NSMutableDictionary *child2plist=[NSMutableDictionary dictionary];
    [child2plist setObject:@"23" forKey:@"age" ];
    [child2plist setObject:@"male" forKey:@"sex" ];
    [child2plist setObject:@"football" forKey:@"hobby" ];
    
    [rootdicplist setObject:child2plist forKey:@"xiaozhang"];
    //写入文件
    [rootdicplist writeToFile:dicplistpath atomically:YES];
    /*结束创建并写入Dictionary键值plist*/
    

7、修改string类型plist

代码如下:

 /*对string plist文件的修改*/
    NSArray *changepath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *changestringpath=[changepath objectAtIndex:0];
    NSString *endchangepath=[changestringpath stringByAppendingPathComponent:@"stringinfo.plist"];
    NSMutableDictionary *mutablestring=[[NSMutableDictionary alloc] initWithContentsOfFile:endchangepath];
    NSString *age=[mutablestring objectForKey:@"age"];
    age=@"27";
    [mutablestring setObject:age forKey:@"age"];
    [mutablestring writeToFile:endchangepath atomically:YES];
    [mutablestring release];
    /* end 对string plist文件的修改*/

8、修改Dictionary类型plist

/*对Dictionary plist文件的修改*/
    NSString *changedicpath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"dicinfo.plist"];
    NSMutableDictionary *dicinfolist=[[NSMutableDictionary alloc] initWithContentsOfFile:changedicpath];
    //获取小张的信息
    NSMutableDictionary *dicuser=[dicinfolist objectForKey:@"xiaozhang"];
    [dicuser setObject:@"female" forKey:@"sex"];
    [dicinfolist setObject:dicuser forKey:@"xiaozhang"];
    [dicinfolist writeToFile:changedicpath atomically:YES];

示例下载地址:下载


抱歉!评论已关闭.