plist是:全名Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的,只能存储NSArray、NSDictionary、NSString、NSData、NSDate、NSNumber类型数据,而且一般我们我会把NSArray、NSDictionary、NSString、NSData、NSDate、NSNumber都存储在一个字典里。
plist文件用于储存少量数据,读取速度快,如果数据量太多,耗费内存太大,读取速度会很慢,应该储存在数据库中。
1.plist文件的创建
//获取Document路径
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
//将创建的plist文件的名称拼接到路径后面,plist文件的名称
NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];
//创建plist文件
//判断当前是不是存在test.plist文件
if([[NSFileManager defaultManager] fileExistsAtPath:filename])
{
//存在直接进行操作
}else{
//创建plist文件
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:filename contents:nil attributes:nil];
}
2.对plist进行操作
//为plist文件写入数据
NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"love",@"name",@"20",@"age",nil];
[dic writeToFile:filename atomically:YES];
//读取plist文件中的数据
NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
NSLog(@"dic is:%@",dic2);
//修改plist文件中的数据
[dic2 setObject:@"you" forKey:@"name"];
[dic2 writeToFile:filename atomically:YES];
//删除plist文件
[fileManager removeItemAtPath:filename error:nil];
以上为plist文件的使用,