iOS 数据持久化 plist

- (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);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值