一 .plist 文件的读写
//获取路径对象
NSArray*pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString*path = [pathArray objectAtIndex:0];
//获取文件的完整路径
NSString*filePatch = [pathstringByAppendingPathComponent:@"column.plist"];
NSLog(@"%@",filePatch);
//写入数据到plist文件
NSMutableDictionary*dic1= [NSMutableDictionarydictionaryWithObjectsAndKeys:@"小小虎",@"name",@"5",@"age",@"boy",@"sex",nil];
NSMutableDictionary*dic2= [NSMutableDictionarydictionaryWithObjectsAndKeys:@"小小兮",@"name",@"6",@"age",@"girl",@"sex",nil];
//将上面2个小字典保存到大字典里面
NSMutableDictionary*dataDic = [NSMutableDictionary dictionary];
[dataDicsetObject:dic1forKey:@"一年级"];
[dataDicsetObject:dic2forKey:@"二年级"];
//写入plist里面
[dataDicwriteToFile:filePatchatomically: YES];
//读取plist文件的内容
NSMutableDictionary*dataDictionary = [[NSMutableDictionaryalloc]initWithContentsOfFile: filePatch];
NSLog(@"---plist一开始保存时候的内容---%@",dataDictionary);
二 .对plist 文件进行修改
//修改字典里面的内容,先按照结构取到你想修改内容的小字典
NSMutableDictionary*dd = [dataDictionary objectForKey:@"一年级"];
[ddsetObject:@"我改名字了哦"forKey:@"name"];
[ddsetObject: @"我添加的新内容"forKey: @"content"];
[ddremoveObjectForKey: @"age"];
//修改成功以后,将这个小字典重新添加到大字典里面
[dataDictionary setObject: ddforKey:@"一年级"];
[dataDictionary writeToFile: filePatchatomically: YES];
NSLog(@"---plist做过操作之后的字典里面内容---%@",dataDictionary);
三 .文件的删除
//清除plist文件,可以根据我上面讲的方式进去本地查看plist文件是否被清除
NSFileManager*fileMger = [NSFileManager defaultManager];
NSString*xiaoXiPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)objectAtIndex:0]stringByAppendingPathComponent:@"xiaoxi.plist"];
//如果文件路径存在的话
BOOLbRet = [fileMgerfileExistsAtPath: xiaoXiPath];
if(bRet) {
NSError*err;
[fileMgerremoveItemAtPath: xiaoXiPatherror: &err];
}