属性文件Plist

属性文件:
Property List File:简称plist
概念
iOS开发中常见的一种文件格式。按照固定格式保存数据。
属性文件和XML文件都属性结构化文件。文件的内容按指定的格式保存数据。
作用:
存储数据:项目中的一些配置,不经常变化的数据
可存储的类型:
数组:基本类型
字典:Key必须是字符串,value基本类型
怎么产生属性文件
 1. 手工写创建的plist文件,属于程序的文件,保存在应用程序包中
2. 程序生成,需要指定文件保存路径
使用NSArray或NSDictionary对象生成plist文件
 
写入三步法:
step1:准备文件路径
step2:准备文件内容
step3:写入文件,不用创建文件就可写入
用数组写入
//准备文件路径
NSString *filePath = [self.documentsPath stringByAppendingPathComponent:@"a.plist"];
NSLog(@"%@", filePath);
//准备文件内容
NSArray *contents = @[@"Daniel", @"Guodh", @"Shasha", @"Shanshan"];
//写入文件,数据写入文件即成plist文件
[contents writeToFile:filePath atomically:YES];
用字典写入
//准备路径
NSString *filePath = [self.documentsPath stringByAppendingPathComponent:@"person.plist"];
//准备内容(Dictionary)
NSDictionary *contents = @{ @"ID":@"1001", @"name":@"张三", @"age":@23, @"gender":@YES};
//写入到plist
[contents writeToFile:filePath atomically:YES];
 
读取:
步骤:
step1:准备文件路径
step2:读取文件
读取应用程序的plist文件
//获取plist文件的路径
NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"names" ofType:@"plist"];
//读取数据到数组
NSArray *names = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"names:%@", names);
//使用字典读取
NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:plistPath];
 
 
//更多读取可以看:数据读取写入操作
 
原文地址:https://www.cnblogs.com/lignpeng/p/5458338.html