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(@"文件刪除成功");
}
