Plist文件操作:2-Plist文件的读取
Plist文件的顶层结构通常是由一个字典或者一个数组构成的,因此,Plist文件的读取思路,即把Plist文件中的所有数据一次性读取出来,然后放到一个字典对象或数组对象中,供后续操作使用。
读取Plist文件返回数组
当Plist文件的顶层结构是一个数组时,通常会用于存储一些具有相同数据结构的数据,这个结构与数据库的表是类似的。
- 手工添加一个Plist文件,顶层结构是一个Array,然后再添加两条记录
- 获取plist文件的路径
NSString *arrayPistPath = [[NSBundle mainBundle] pathForResource:@"websites" ofType:@"plist"];
- 使用arrayWithContentsOfFile:方法,读取Plist文件,结果存放在一个数组对象中
+ (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;
NSMutableArray *arrayM = [NSMutableArray arrayWithContentsOfFile:arrayPistPath];
NSLog(@"%@",arrayM);
运行结果:
读取Plist文件返回字典
当Plist文件的顶层结构是一个字典时,其中存储了多个键值对,通常可以用于保存一些App运行的配置信息,另外,有关配置信息我们也可以保存在偏好设置中,实现原理完全相同。
- 手工添加一个Plist文件,顶层结构是一个字典
- 获取Plist文件的路径
NSString *dicPistPath = [[NSBundle mainBundle] pathForResource:@"website" ofType:@"plist"];
- 使用dictionaryWithContentsOfFile:方法,读取Plist文件,结果存放在一个字典对象中
+ (nullable NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:dicPistPath];
NSLog(@"%@",dict);
运行结果:
示例代码
https://github.com/99ios/13.2.2
文章发布时间为: November 4th , 2016 at 09:36 am
最后编辑时间为: September 15th , 2017 at 05:21 am
本文由 99ios 创作,转载请注明出处
最后编辑时间为: September 15th , 2017 at 05:21 am
本文由 99ios 创作,转载请注明出处