属性列表(Property List)文件是一种用来存储序列化后的对象的文件。属性列表文件的文件拓展名为.plist,因此通常被称为plist文件。
在沙盒中新建plist文件:
@implementation ViewController
{
NSMutableArray *markMutArray;
UITextField *textField;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initPlistData];
[self getAndAddContent];
}
//
- (void)getAndAddContent
{
textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 60, 150, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
UIButton *addbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addbutton.frame = CGRectMake(230, 60, 100, 40);
[addbutton setTitle:@"添加内容" forState:UIControlStateNormal];
[addbutton addTarget:self action:@selector(clickAddContent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:addbutton];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 200, 90, 50);
[button setTitle:@"读取内容" forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickGetCont) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//点击button添加内容
- (void)clickAddContent
{
[markMutArray addObject:textField.text];
NSDictionary *dic = @{@"分类": markMutArray};
[dic writeToFile:[self dataFilePath] atomically:YES];
}
//点击button读取内容
- (void)clickGetCont
{
NSString *filePath = [self dataFilePath];
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *getDataArray = [dic objectForKey:@"分类"];
NSLog(@"getDataArray:%@", getDataArray);
}
//初始化内容
- (void) initPlistData
{
//1.首先获取文件的存取路径
NSString *filePath = [self dataFilePath];
NSLog(@"文件路径---%@", filePath);
//检查数据文件是否存在,如果不存在就不加载它了,如果存在,就用该文件的内容实例化数组,然后将数组中的对象复制到4个文本字段。
//由于是按顺序排列的列表,因此只要根据保存顺序来复制数组,就一定可以确保相应的字段获得正确的值
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *arrayMark = [dic objectForKey:@"分类"];
markMutArray = [NSMutableArray arrayWithArray:arrayMark];
} else {
markMutArray = [NSMutableArray arrayWithObjects:@"默认分类", @"新建分类", nil];
}
//4.将可变数组里面的数据按照路径写入文件
NSDictionary *dic = @{@"分类" : markMutArray};
[dic writeToFile:filePath atomically:YES];
}
//获取文件的路径
- (NSString*)dataFilePath
{
//1.获取文件路径数组,这个是因为考虑到为mac编写代码的原因
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //注意,括号里面的第一个元素是NSDocumentDirectory,而不是NSDocumentationDirectory
//2.获取文件路径数组的第一个元素,因为我们只需要这一个
NSString *documentPath = [paths objectAtIndex:0];
//3.获取第2步得到的元素的字符串,并创建一个名为data.plist的.plist文件用于保存数据
NSString *fileName = [documentPath stringByAppendingPathComponent:@"data.plist"];
return fileName;
}
//点击键盘及编辑区域外区域收起键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (![textField isExclusiveTouch]) {
[textField resignFirstResponder];
}
}
command+n快捷键新建,选择Resource中的Property Plist新建一个plist文件:
plist文件中事先添加好的内容:
plist文件的读取:
//读取plist文件
//获取plist文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//字典dic的key为:sort,value为一个数组
NSArray *dataArray = [NSArray arrayWithArray:[dic objectForKey:@"sort"]];
NSLog(@"plist:%@", dataArray);
Plist文件只能存储NSString NSNumber NSData NSArray NSDictionary的内容,其文件存储为xml格式
各种不同格式存储内容的存储方式参考博客:
http://www.bkjia.com/Androidjc/827862.html
原文:http://www.cnblogs.com/Ruoxin/p/4460048.html