標籤:
對plist檔案進行讀寫
//擷取路徑對象 NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [pathArray objectAtIndex:0]; //擷取檔案的完整路徑 NSString *filePatch = [path stringByAppendingPathComponent:@"column.plist"]; NSLog(@"%@",filePatch); //寫入資料到plist檔案 NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"小小虎",@"name",@"5",@"age",@"boy",@"sex",nil]; NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"小小兮",@"name",@"6",@"age",@"girl",@"sex",nil]; //將上面2個小字典儲存到大字典裡面 NSMutableDictionary *dataDic = [NSMutableDictionary dictionary]; [dataDic setObject:dic1 forKey:@"一年級"]; [dataDic setObject:dic2 forKey:@"二年級"]; //寫入plist裡面 [dataDic writeToFile:filePatch atomically:YES]; //讀取plist檔案的內容 NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePatch]; NSLog(@"---plist一開始儲存時候的內容---%@",dataDictionary);
對pilst檔案進行修改
//修改字典裡面的內容,先按照結構取到你想修改內容的小字典 NSMutableDictionary *dd = [dataDictionary objectForKey:@"一年級"]; [dd setObject:@"我改名字了哦" forKey:@"name"]; [dd setObject:@"我添加的新內容" forKey:@"content"]; [dd removeObjectForKey:@"age"]; //修改成功以後,將這個小字典重新添加到大字典裡面 [dataDictionary setObject:dd forKey:@"一年級"]; [dataDictionary writeToFile:filePatch atomically:YES]; NSLog(@"---plist做過操作之後的字典裡面內容---%@",dataDictionary);
刪除
//清除plist檔案,可以根據我上面講的方式進去本地查看plist檔案是否被清除 NSFileManager *fileMger = [NSFileManager defaultManager]; NSString *xiaoXiPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"xiaoxi.plist"]; //如果檔案路徑存在的話 BOOL bRet = [fileMger fileExistsAtPath:xiaoXiPath]; if (bRet) { NSError *err; [fileMger removeItemAtPath:xiaoXiPath error:&err]; }
iOS--對plist檔案進行讀寫,增刪改查