plist文件的生成

一、 通过数组生成plist文件

  NSArray *arr = @[@"hello", @1, @4, @{@"one":@1}];
        //参数2:YES 表示会先将数据放入缓存,然后再写入文件;NO 表示直接将数据写入文件
        BOOL ret = [arr writeToFile:@"文件路径/arr.plist" atomically:YES];
        if (ret) {
            NSLog(@"success");
        }else{
            NSLog(@"failed");
        }
        
        NSArray *arr1 = [[NSArray alloc] initWithContentsOfFile:@"文件路径/arr.plist"];
        NSLog(@"%@", arr1);
        NSDictionary *dic = arr1[3];
        NSLog(@"%@", dic[@"one"]);

二、通过字典数据生成plist文件

 NSDictionary *dic1 = @{@"one":@"hello",
              @"two":@123,
              @"four":@[@"hello", @"world"]};
        [dic1 writeToFile:@"文件路径/dic.plist" atomically:YES];
        
        NSDictionary *dic2 = [[NSDictionary alloc] initWithContentsOfFile:@"文件路径/dic.plist"];
        NSLog(@"%@", dic2);
        
        //修改数据
        NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithContentsOfFile:@"文件路径/dic.plist"];
        [mDic setObject:@"world" forKey:@"one"];
        NSLog(@"%@", mDic);
        [mDic writeToFile:@"文件路径/dic.plist" atomically:YES];

你可能感兴趣的:(plist文件的生成)