iOS 数据持久化 plist

来源:互联网 发布:截图下载软件 编辑:程序博客网 时间:2024/06/05 18:13

- (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 = [docstringByAppendingPathComponent:@"guoyule.plist"];

   NSLog(@"%@",path);

   NSArray * arr = @[@"guoyule",@"24"];

    [arr writeToFile:pathatomically:YES];

    NSDictionary * dict =@{@"name":@"guoyule",@"age":@"28"};

    [dict writeToFile:pathatomically:YES];

   /*

     plist只能存储系统自带的一些常规的类,也就是有writeToFile方法的对象才可以使用plist保存数据

     字符串/字典/数据/NSNumber/NSData...

     

     */

    //自定义的对象不可以保存到plist

    

}


- (IBAction)readBtn:(id)sender {

    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject];

    

    NSString *path = [docstringByAppendingPathComponent:@"guoyule.plist"]

    ;

    // 读取数据

    NSDictionary *dict = [NSDictionarydictionaryWithContentsOfFile:path];

   NSLog(@"%@", dict);

}


0 0
原创粉丝点击