IOS Plist文件操作之寫入/讀取/刪除

ngmm 9年前發布 | 7K 次閱讀 Objective-C IOS

1.保存在user Document文件夾下,以讀取文件,寫入文件方式 2.在工程里手動創建一個.plist文件,把固定的內容寫入,這個需要人工手動寫入(工程里只可讀取,不可以寫入) 3.保存在user Document下,不過不需要讀寫文件,用系統的 NSUserDefaults 可以快速保存添加讀取刪除基本數據類型 這里記錄的是第1種,第2種就是創建一個plist文件,然后自己手動寫入數據,再用NSString *path = [[NSBundle mainBundle] pathForResource:@"xiaoxi" ofType:@"plist"];獲取到本地存儲的數據。

  • 寫入數據到plist文件

  •   //獲取路徑對象
        NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [pathArray objectAtIndex:0];
        //獲取文件的完整路徑
        NSString *filePatch = [path stringByAppendingPathComponent:@"xiaoxi.plist"];
        
        //上面3句可以寫成這一句
    //    NSString *filePatch = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"xiaoxi.plist"];
        
    //    NSLog(@"------filepath---%@",filePatch);
        /*
         *
         下面是我的plist路徑,在桌面空白處點擊一下,前往-按住option-資源庫-Developer-CoreSimulator-Devices......就按照下面路徑找到plist所在的位置
         *
        /Users/baiteng01/Library/Developer/CoreSimulator/Devices/92444384-5241-4934-B078-1A7241F1B687/data/Containers/Data/Application/73005382-D1FB-4BC2-BB4E-1FBC64284141/Documents/xiaoxi.plist
         *
         */
        
        //寫入數據到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);
  • 對plist文件內容進行/刪除/修改/添加/寫入操作

  •  //修改字典里面的內容,先按照結構取到你想修改內容的小字典
        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文件,可以根據我上面講的方式進去本地查看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];
        }


 本文由用戶 ngmm 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!