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

  

  

原文地址:https://www.cnblogs.com/dzq1991/p/6246998.html