标签:
plist文件的格式
plist是一种数据持久化的文件
1、最外层通常为数组或者字典
2、plist里面的数据 只局限于数组、字典、逻辑值(BOOL)、NSNumber、NSData、NSDate、字符串等数据类型
3、无法存储自定义类型的对象
plist作用
1、不适用它作为缓存 无法存储自定义类型的对象
2、通常存储长时间不容易发生变化的数据。国家、省市区、汽车品牌、项目的info.plist工程的设置信息
3、占用内存小
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//plist是 数组和字典的套用关系来存储数据的一种文件
//创建一个字典 字面有数组、字符串等等
NSString * string1 = @"snail";
NSString * string2 = @"jj";
NSArray * array = @[@"one",@"two",@"three"];
NSDictionary * dict = @{@"SNAIL":string1,
@"JJ":string2,
@"ARRAY":array
};
//用字典生成plist文件
BOOL ret = [dict writeToFile:@"/Users/Snail/Desktop/snail/myPlist.plist" atomically:YES];
if (ret) {
NSLog(@"生成成功");
}else{
NSLog(@"生成失败");
}
//因为我们最终是以NSDictionary来生成plist文件的,所以再读plist文件的时候,首先它是一个字典
NSDictionary * dict1 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/Snail/Desktop/snail/myPlist.plist"];
/*----------dict1的输出格式
{
ARRAY = (
one,
two,
three
);
JJ = jj;
SNAIL = snail;
}
*/
NSLog(@"%@",dict1);
//用数组来生成plist文件
NSArray * array1 = @[
//字符串数组
@[@"one",@"two",@"three"],
//字符串
@"Snail",
//字典
@{@"name":@"Snail",@"age":@10}
];
[array1 writeToFile:@"/Users/Snail/Desktop/snail/myPlist1.plist" atomically:YES];
//读plist文件
NSArray * arr = [NSArray arrayWithContentsOfFile:@"/Users/Snail/Desktop/snail/myPlist1.plist"];
/*----------可以看出输出的是一个数组、一个字符串、字典
(
one,
two,
three
),
Snail,
{
age = 10;
name = Snail;
}
*/
NSLog(@"%@",arr);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq1791422018/article/details/47106759