- (IBAction)saveBtn:(id)sender {
// 获取沙盒路径
NSString * home = NSHomeDirectory();
// 不建议这么写因为这样就会直接写死 将来 苹果公司改变了路径名字 那就惨了
// NSString * path = [home stringByAppendingString:@"/Documents"];
// NSString * path = [home stringByAppendingPathComponent:@"Documents"];
//建议使用动态加载方式 因为苹果公司的加载方法一般不会变
// NSUserDomainMask 在用户目录下查找
// NSDocumentDirectory 查找Documents文件夹
// YES 代表用户目录的~
NSString * doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
// 拼接文件路径
NSString * path = [doc stringByAppendingPathComponent:@"guoyule.plist"];
NSLog(@"%@",path);
NSArray * arr = @[@"guoyule",@"24"];
[arr writeToFile:path atomically:YES];
NSDictionary * dict = @{@"name":@"guoyule",@"age":@"28"};
[dict writeToFile:path atomically:YES];
/*
plist只能存储系统自带的一些常规的类, 也就是有writeToFile方法的对象才可以使用plist保存数据
字符串/字典/数据/NSNumber/NSData...
*/
//自定义的对象不可以保存到plist中
}
- (IBAction)readBtn:(id)sender {
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"guoyule.plist"]
;
// 读取数据
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@", dict);
}