3.4-文件操作

iOS与沙盒文件操作

一项目文件

  1保存项目文件:拖文件

  2读取项目文件,需要使用NSBundle对象

  

        //获取plist文件路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"tom.plist" ofType:nil];
        //用字典读取plist数据
        _tomData = [NSDictionary dictionaryWithContentsOfFile:filePath];

   

二沙盒文件

  1保存沙盒文件

    

    // 获得douments文件夹
    NSString *doc = [ NSHomeDirectory()  stringByAppendingPathComponent:@"Documents"];
    // 获得保存数据的文件路径
    NSString *filePath = [doc stringByAppendingPathComponent:@"abc"];
    NSDictionary *data = @{@"name":@"jack",@"age":@100,@"height":@1.7};
    // 只要能够调用writeToFile方法的对象,都能够保存plist文件中。
    // NSString/NSData/NSNumber/NSDictionary/NSArray
    [@"1111" writeToFile:filePath atomically:YES];

 

  2读取沙盒文件

  

    // 获得保存数据的文件路径
    NSString *filePath = [doc stringByAppendingPathComponent:@"abc.plist"];
    NSDictionary *dict =[NSDictionary dictionaryWithContentsOfFile:filePath];

 

  

  

转载于:https://www.cnblogs.com/dzq1991/p/6246998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值