Plist文件的读取与写入

1.创建plist文件command+n -> Resourece -> Property List.创建完成就可以进行在里面手动写入数据 如图:

Plist文件的读取与写入_第1张图片

2.读取plist

NSString *path = [[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"plist"];

NSMutableDictionary *dataDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

DLog(@"%@",dataDic);

DLog(@"%@",[[dataDic objectForKey:@"CH"] objectForKey:@"BJ"]);

结果:

Plist文件的读取与写入_第2张图片

3.往plist中添加内容

代码如下:

// 添加一项内容

[dataDic setObject:@"伦敦" forKey:@"EN"];

//获取应用程序沙盒的Documents目录

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *plistPath1 = [paths objectAtIndex:0];

//得到完整的文件名

NSString *filename=[plistPath1 stringByAppendingPathComponent:@"test.plist"];

//写入

[dataDic writeToFile:filename atomically:YES];

NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];

DLog(@"%@",data1);

结果:

Plist文件的读取与写入_第3张图片

你可能感兴趣的:(Plist文件的读取与写入)