1.plist,全名PropertyList,即属性列表文件,它是一种用来存储串行化后的对象的文件。这种文件,在ios开发过程当中常常被用到。这种属性列表文件的扩展名为.plist,所以一般被叫作plist文件。文件是xml格式的。Plist文件是以key-value的形式来存储数据。既能够用来存储用户设置,也能够用来存储一些须要常常用到而不常常改动的信息。linux
在对plist文件的操做有建立,删除,写入和读取。这四种操做中,写入和读取是比较经常使用的操做。ios
2.下面我对这四种操做进行一一的陈述。数组
首先,是怎么去建立plist文件。Plist文件的建立既能够经过在程序中经过新建文件的方式来建立,也能够经过在程序中用代码的形式来建立文件。atom
第一种就是经过新建文件,在弹出的窗口中选择ios项目下的Resource中的Property List来进行plist文件的建立。而后点击TestPlistDemo.plist文件,出现一个Root行,点击Root这一行,而后经过点击右键->Add Row或者点击Root后面的加号来增长一行。这一行中包含三个属性,key、type、value。其中key是字段属性,type是字段类型,value是字段对应的值。而Type又包含7中类型,其中两种是Array和Dictionary,这两种是数组的形式,在它们下面还能够包含许多key-value。xml
而另外5种是Boolean,data,string,date,number。这5种类型的数据都是被array和dictionary所要包含的数据。对象
经过代码来建立plist文件,代码以下:开发
//创建文件管理rem
NSFileManager *fm = [NSFileManager defaultManager];get
//找到Documents文件所在的路径string
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUSErDomainMask, YES);
//取得第一个Documents文件夹的路径
NSString *filePath = [path objectAtIndex:0];
//把TestPlist文件加入
NSString *plistPath = [filePath stringByAppendingPathComponent:@"test.plist"];
//开始建立文件
[fm createFileAtPath:plistPath contents:nil attributes:nil];
//删除文件
[fm removeItemAtPath:plistPath error:nil];
在写入数据以前,须要把要写入的数据先写入一个字典中,建立一个dictionary:
//建立一个字典
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2", nil];
//把数据写入plist文件
[dic writeToFile:plistPath atomically:YES];
读取plist中的数据,形式以下:
//读取plist文件,首先须要把plist文件读取到字典中
NSDictionary *dic2 = [NSDictionary dictionaryWithContentsOfFile:plistPath];
//打印数据
NSLog(@"key1 is %@",[dic2 valueForKey:@"1"]);
NSLog(@"dic is %@",dic2);
http://www.linuxidc.com
关于plist中的array读写,代码以下:
//把TestPlist文件加入
NSString *plistPaths = [filePath stringByAppendingPathComponent:@"tests.plist"];
//开始建立文件
[fm createFileAtPath:plistPaths contents:nil attributes:nil];
//建立一个数组
NSArray *arr = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil];
//写入
[arr writeToFile:plistPaths atomically:YES];
//读取
NSArray *arr1 = [NSArray arrayWithContentsOfFile:plistPaths];
//打印
NSLog(@"arr1is %@",arr1);