3.4-文件操作

一项目文件

  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];

 

  

  

posted @ 2017-01-04 00:01  行者烧炉子  阅读(114)  评论(0编辑  收藏  举报