1.就是手动创建.plist文件。
选中Xcode,使用快捷方式command+N
注意:只能读取,不能用代码进行更删改查。。(如果本身plist文件在bundle中是无法修改的,需要先从Bundle中移出才可以。你应该把Plist文件从bundle复制到cache目录下,然后数据就可以发生改变。)
//获取已有完整路径
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MY_PropertyList" ofType:@"plist"];
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
//读取手动创建的plist文件的属性的值。
NSString *name = [usersDic valueForKey:@"name"];
NSString * password = [usersDic valueForKey:@"password"];
NSLog(@"读取手动创建的plist文件的属性的值 ====%@======%@",name,password);
2.就是用代码创建.plist文件。
//获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取完整路径
NSString *documentsPath = [path objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"MY_PropertyList.plist"];
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
//设置属性值
[usersDic setObject:@"孙悟空" forKey:@"name"];
[usersDic setObject:@"sunwukong" forKey:@"password"];
//写入文件
[usersDic writeToFile:plistPath atomically:YES];
运行后在沙盒中找到该文件的位置打开
修改用代码创建.plist文件 的属性
//获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取完整路径
NSString *documentsPath = [path objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"MY_PropertyList.plist"];
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];
//设置属性值
[usersDic setObject:@"孙大圣" forKey:@"name"];
[usersDic setObject:@"sunwukong" forKey:@"password"];
//写入文件
[usersDic writeToFile:plistPath atomically:YES];