iOS--對plist檔案進行讀寫,增刪改查

來源:互聯網
上載者:User

標籤:

對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檔案進行讀寫,增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.