Plist文件的读取
先看一下plist的文件结构
也可以通过鼠标右键中的OpenAs-Source Code查看到plist的另一种结构,和常用的xml差不多
最后看看怎么读取plist资源文件
Bundle *bundle = [NSBundle mainBundle];
// 设置资源文件名称为Property List,资源类型为plist
NSString *path = [bundle pathForResource:@"Property List" ofType:@"plist"];
// 在plist中配置了一个NSDictionary字典,字典key为user
NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfFile:path] objectForKey:@"user"];
// 在字典中有一个key为aaa的NSArray
NSArray *list = [dict objectForKey:@"user_1"];
for (int i = 0, len = [list count]; i < len; i++) {
NSLog(@"%@", [list objectAtIndex:i]);
}
Plist文件的写入
获得沙盒中Document文件夹
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.pathName = [document stringByAppendingPathComponent:@"test.plist"];
设置NSMutableDictionary的值,最后writeToFile写入到plist文件中
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:self.nameText.text forKey:@"name"];
[dict setObject:self.ageText.text forKey:@"age"];
[dict setObject:self.hobbyText.text forKey:@"hobby"];
[dict writeToFile:self.pathName atomically:YES];