一 .plist 文件的讀寫
//獲取路徑對(duì)象
NSArray*pathArray?=?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,YES);
NSString*path?=?[pathArray objectAtIndex:0];
//獲取文件的完整路徑
NSString*filePatch?=?[pathstringByAppendingPathComponent:@"column.plist"];
NSLog(@"%@",filePatch);
//寫入數(shù)據(jù)到plist文件
NSMutableDictionary*dic1=?[NSMutableDictionarydictionaryWithObjectsAndKeys:@"小小虎",@"name",@"5",@"age",@"boy",@"sex",nil];
NSMutableDictionary*dic2=?[NSMutableDictionarydictionaryWithObjectsAndKeys:@"小小兮",@"name",@"6",@"age",@"girl",@"sex",nil];
//將上面2個(gè)小字典保存到大字典里面
NSMutableDictionary*dataDic?=?[NSMutableDictionary dictionary];
[dataDicsetObject:dic1forKey:@"一年級(jí)"];
[dataDicsetObject:dic2forKey:@"二年級(jí)"];
//寫入plist里面
[dataDicwriteToFile:filePatchatomically: YES];
//讀取plist文件的內(nèi)容
NSMutableDictionary*dataDictionary?=?[[NSMutableDictionaryalloc]initWithContentsOfFile: filePatch];
NSLog(@"---plist一開(kāi)始保存時(shí)候的內(nèi)容---%@",dataDictionary);
二 .對(duì)plist 文件進(jìn)行修改
//修改字典里面的內(nèi)容,先按照結(jié)構(gòu)取到你想修改內(nèi)容的小字典
NSMutableDictionary*dd?=?[dataDictionary ?objectForKey:@"一年級(jí)"];
[ddsetObject:@"我改名字了哦"forKey:@"name"];
[ddsetObject: @"我添加的新內(nèi)容"forKey: @"content"];
[ddremoveObjectForKey: @"age"];
//修改成功以后帝蒿,將這個(gè)小字典重新添加到大字典里面
[dataDictionary ?setObject: ddforKey:@"一年級(jí)"];
[dataDictionary ?writeToFile: filePatchatomically: YES];
NSLog(@"---plist做過(guò)操作之后的字典里面內(nèi)容---%@",dataDictionary);
三 .文件的刪除
//清除plist文件闯割,可以根據(jù)我上面講的方式進(jìn)去本地查看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];
}