IOS 对plist文件的读写

来源:互联网 发布:淘宝同城发货不用快递 编辑:程序博客网 时间:2024/05/21 17:38

在做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文件数据

读取代码如下:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /*对plist文件的读*/  
  2.    NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"schoolinfo" ofType:@"plist"];  
  3.    NSMutableDictionary *data=[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];  
  4.    NSLog(@"%@",data);  
  5.    /*读取数据结束*/  

显示如下:


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

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //在plist中追加写入数据  
  2.     [data setObject:@"20" forKey:@"count"];  
  3.     [data writeToFile:plistPath atomically:YES];//保存  
追加数据以后显示如下:


4、创建与写plist文件

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

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /*保存数据一*/  
  2.    /*新建一个plist*/  
  3.    //获取应用程序沙盒的Documents目录  
  4.    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
  5.    NSString *newplistPath = [paths objectAtIndex:0];  
  6.    //得到完整的文件名  
  7.    NSString *filename=[newplistPath stringByAppendingPathComponent:@"task.plist"];  
  8.    //保存数据  
  9.    [data writeToFile:filename atomically:YES];  
  10.    /*保存数据一结束*/  
  11.      
  12.    //将保存的数据读出来  
  13.    NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];  
  14.    NSLog(@"%@", data1);  

5、创建string类型plist

代码如下:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /*写入string数据*/  
  2.     //获取路径对象  
  3.     NSArray *path1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  4.     NSString *documentpath=[path1 objectAtIndex:0];  
  5.     NSString *stringplistpath=[documentpath stringByAppendingPathComponent:@"stringinfo.plist"];  
  6.     NSMutableDictionary *dicplist=[NSMutableDictionary dictionary];  
  7.     //设置属性  
  8.     [dicplist setObject:@"20" forKey:@"age"];  
  9.     [dicplist setObject:@"male" forKey:@"sex"];  
  10.     [dicplist setObject:@"sports" forKey:@"hobby"];  
  11.     //写入文件  
  12.     [dicplist writeToFile:stringplistpath atomically:YES];  
  13.     /*结束写入string数据*/  

6、创建Dictionary类型plist

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /*创建并写入Dictionary键值plist*/  
  2. NSArray *path2=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  3. NSString *documentpath2=[path2 objectAtIndex:0];  
  4. NSString *dicplistpath=[documentpath2 stringByAppendingPathComponent:@"dicinfo.plist"];  
  5.   
  6. NSMutableDictionary *rootdicplist=[NSMutableDictionary dictionary];  
  7.   
  8. //定义第一个Dictionary集合  
  9. NSMutableDictionary *child1plist=[NSMutableDictionary dictionary];  
  10. [child1plist setObject:@"25" forKey:@"age"];  
  11. [child1plist setObject:@"female" forKey:@"sex"];  
  12. [child1plist setObject:@"basketball" forKey:@"hobby"];  
  13.   
  14. //添加到根集合中  
  15. [rootdicplist setObject:child1plist forKey:@"xiaohua"];  
  16.   
  17. //定义第二个Dictionary集合  
  18. NSMutableDictionary *child2plist=[NSMutableDictionary dictionary];  
  19. [child2plist setObject:@"23" forKey:@"age" ];  
  20. [child2plist setObject:@"male" forKey:@"sex" ];  
  21. [child2plist setObject:@"football" forKey:@"hobby" ];  
  22.   
  23. [rootdicplist setObject:child2plist forKey:@"xiaozhang"];  
  24. //写入文件  
  25. [rootdicplist writeToFile:dicplistpath atomically:YES];  
  26. /*结束创建并写入Dictionary键值plist*/  

7、修改string类型plist

代码如下:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /*对string plist文件的修改*/  
  2.    NSArray *changepath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  3.    NSString *changestringpath=[changepath objectAtIndex:0];  
  4.    NSString *endchangepath=[changestringpath stringByAppendingPathComponent:@"stringinfo.plist"];  
  5.    NSMutableDictionary *mutablestring=[[NSMutableDictionary alloc] initWithContentsOfFile:endchangepath];  
  6.    NSString *age=[mutablestring objectForKey:@"age"];  
  7.    age=@"27";  
  8.    [mutablestring setObject:age forKey:@"age"];  
  9.    [mutablestring writeToFile:endchangepath atomically:YES];  
  10.    [mutablestring release];  
  11.    /* end 对string plist文件的修改*/  

8、修改Dictionary类型plist

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /*对Dictionary plist文件的修改*/  
  2.     NSString *changedicpath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"dicinfo.plist"];  
  3.     NSMutableDictionary *dicinfolist=[[NSMutableDictionary alloc] initWithContentsOfFile:changedicpath];  
  4.     //获取小张的信息  
  5.     NSMutableDictionary *dicuser=[dicinfolist objectForKey:@"xiaozhang"];  
  6.     [dicuser setObject:@"female" forKey:@"sex"];  
  7.     [dicinfolist setObject:dicuser forKey:@"xiaozhang"];  
  8.     [dicinfolist writeToFile:changedicpath atomically:YES];  

示例下载地址:下载

0 0
原创粉丝点击