iOS開發-plist檔案增刪改查
阿新 • • 發佈:2019-01-30
說到plist檔案,很直接的想法就是把資料已經編譯到手機上,非常的簡單省時間。。。但是有個問題 我也是待驗證 是不是程式碼操作可以刪除目錄下的plist檔案 這個待驗證哦!
plist第一次看到這個字尾名檔案的時候感覺怪怪的,不過接觸久了也就習以為常了,plist是Property List的簡稱可以理解成屬性列表檔案,主要用來儲存序列化後的物件的檔案。副檔名為.plist,因此被稱為 plist檔案,xCode中預設的是一種樹狀的結構展現出來資料,視覺化的動態增刪改查,非常人性化,不過最終的結果是以XML形式儲存的,Plist檔案可以用於儲存使用者的一些設定資訊,具體根據需求而定。
簡單建立檔案
簡單建立檔案就是說可以直接從xCode建立,右擊專案new File,可以新增一個plist檔案:
建立一個UserData.plist檔案,之後的內容如下:
右擊open
as->source code,程式碼如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Login</key> <dict> <key>UserName</key> <string>FlyElephant</string> <key>UserPassWord</key> <string>123456</string> </dict> </dict> </plist>
讀取設定的資訊:
//讀取Property List檔案 NSString *userDataPath = [[NSBundle mainBundle] pathForResource:@"UserData" ofType:@"plist"]; NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:userDataPath]; NSLog(@"%@",data); NSLog(@"使用者名稱:%@ 密碼:%@", data[@"Login"][@"UserName"],data[@"Login"][@"UserPassWord"]); [data setObject:@"登入資訊" forKey:@"Login"];
增刪改查
檔案新增,上面是應用程式中新增檔案,這個時候可以選擇程式碼在沙盒中新增,程式碼如下:NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//獲取完整路徑
NSString *documentsDirectory = [sandboxpath objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"sandbox.plist"];
//儲存根資料
NSMutableDictionary *rootDic = [[NSMutableDictionary alloc ] init];
//字典中的詳細資料
NSMutableDictionary *userDataDic = [[NSMutableDictionary alloc]init];
[userDataDic setObject:@"Flephant" forKey:@"UserName"];
[userDataDic setObject:@"http://www.cnblogs.com/xiaofeixiang/" forKey:@"UserPassWord"];
[rootDic setObject:userDataDic forKey:@"Login"];
//寫入檔案
[rootDic writeToFile:plistPath atomically:YES];
NSLog(@"%@",NSHomeDirectory());
NSLog(@"寫入成功");
路徑如下,具體路徑獲取上一篇文章已經可以看到:
讀取資料:
//獲取路徑
NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[sandboxpath objectAtIndex:0] stringByAppendingPathComponent:@"sandbox.plist"];
NSLog(@"%@",NSHomeDirectory());
//獲取資料
NSMutableDictionary *searchdata = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",searchdata);
修改檔案://獲取路徑
NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"sandbox.plist"];
//所有的資料列表
NSMutableDictionary *datalist= [[[NSMutableDictionary alloc]initWithContentsOfFile:filepath]mutableCopy];
//獲取Login節點
NSMutableDictionary *loginData = [datalist objectForKey:@"Login"];
[loginData setValue: @"FlyElephant" forKey:@"UserName"];
[loginData setValue: @"123456" forKey:@"UserPassWord"];
[datalist setValue:loginData forKey:@"Login"];
[datalist writeToFile:filepath atomically:YES];
NSLog(@"修改成功");
刪除檔案:NSFileManager *manager=[NSFileManager defaultManager];
//檔案路徑
NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"sandbox.plist"];
if ([manager removeItemAtPath:filepath error:nil]) {
NSLog(@"檔案刪除成功");
}
作者:FlyElephant出處:http://www.cnblogs.com/xiaofeixiang
說明:部落格經個人辛苦努力所得,如有轉載會特別申明,部落格不求技驚四座,但求與有緣人分享個人學習知識,生活學習提高之用,部落格所有權歸本人和部落格園所有,如有轉載請在顯著位置給出博文連結和作者姓名,否則本人將付諸法律。