前言
在iOS開(kāi)發(fā)過(guò)程中,我們會(huì)經(jīng)常用到數(shù)據(jù)持久化問(wèn)題,作為數(shù)據(jù)持久化解決方案之一,plist的使用是一個(gè)很方便快捷的方案。
plist保存的地方
1,工程沙盒里(就是程序user Document文件夾下,以讀取文件,寫入文件方式)
2,工程自身里(就是在工程里手動(dòng)創(chuàng)建一個(gè)如.plist文件,把固定的內(nèi)容寫入,這個(gè)需要人工手動(dòng)寫入)
3,工程沙盒里(保存到user Document下,不過(guò)不需要讀寫文件,用系統(tǒng)的 NSUserDefaults 可以快速保存添加讀取刪除基本數(shù)據(jù)類型,類似于android里的Sharedpreferences )
plist是什么?
它全名是:Property List,屬性列表文件,它是一種用來(lái)存儲(chǔ)串行化后的對(duì)象的文件。屬性列表文件的擴(kuò)展名為.plist ,因此通常被稱為 plist文件。文件是xml格式的。Plist文件通常用于儲(chǔ)存用戶設(shè)置,也可以用于存儲(chǔ)捆綁的信息。
如何使用plist——plist的讀取,修改和刪除
plist的讀取(使用在工程自身里的方式)
-
創(chuàng)建一項(xiàng)測(cè)試project1.png
-
創(chuàng)建一個(gè)plist文件
2.png
3.png在plist中添加一些信息
4.png
5.png - 讀取代碼
- (void)viewDidLoad {
[super viewDidLoad];
//讀取plist
[self getDataFromPlist];
}
- (void)getDataFromPlist{
NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"PropertyListTest" ofType:@"plist"];
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
NSLog(@"%@",dataDic);//直接打印數(shù)據(jù)
}
打印結(jié)果
plist的修改寫入
(之前有說(shuō),保存在工程自身的plist并不能修改寫入,所以這里需要通過(guò)沙盒路徑創(chuàng)建plist并修改保存,還有一個(gè)坑,模擬器與真機(jī)權(quán)限可能不一致,在模擬機(jī)上能夠通過(guò)nsbundle路徑修改成功,但是真機(jī)上并不能,所以建議需要修改的plist都使用沙盒路徑來(lái)新建和修改
并且兩者所獲取的plist并不是同一個(gè)文件,代碼解釋如下)
- (void)viewDidLoad {
[super viewDidLoad];
//讀取plist
[self getDataFromPlist];
// 寫入plist
[self writeDataToPlist];
}
- (void)getDataFromPlist{
//沙盒獲取路徑
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathArray objectAtIndex:0];
//獲取文件的完整路徑
NSString *filePatch = [path stringByAppendingPathComponent:@"PropertyListTest.plist"];//沒(méi)有會(huì)自動(dòng)創(chuàng)建
NSLog(@"file patch%@",filePatch);
NSMutableDictionary *sandBoxDataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:filePatch];
if (sandBoxDataDic==nil) {
sandBoxDataDic = [NSMutableDictionary new];
sandBoxDataDic[@"test"] = @"test";
[sandBoxDataDic writeToFile:filePatch atomically:YES];
}
NSLog(@"sandBox %@",sandBoxDataDic);//直接打印數(shù)據(jù)
//工程自身的plist
NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"PropertyListTest" ofType:@"plist"];
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
NSLog(@"nsbundle %@",dataDic);//直接打印數(shù)據(jù)
}
- (void)writeDataToPlist{
//這里使用位于沙盒的plist(程序會(huì)自動(dòng)新建的那一個(gè))
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathArray objectAtIndex:0];
//獲取文件的完整路徑
NSString *filePatch = [path stringByAppendingPathComponent:@"PropertyListTest.plist"];
NSMutableDictionary *sandBoxDataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:filePatch];
NSLog(@"old sandBox is %@",sandBoxDataDic);
sandBoxDataDic[@"test"] = @"hello world";
[sandBoxDataDic writeToFile:filePatch atomically:YES];
sandBoxDataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:filePatch];
NSLog(@"new sandBox is %@",sandBoxDataDic);
//這里使用的是位于工程自身的plist(手動(dòng)新建的那一個(gè))
NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"PropertyListTest" ofType:@"plist"];
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
//開(kāi)始修改及寫入
NSNumber *number = @10;
dataDic[@"1"] = number;//修改
NSNumber *boolNumber = [NSNumber numberWithBool:YES];//bool值只能通過(guò)nsnumber修改
dataDic[@"3"] = boolNumber;
[dataDic writeToFile:plistPath atomically:YES];
//重新獲取數(shù)據(jù) 看是否有變動(dòng)(虛擬機(jī)上會(huì)有變動(dòng),但是真機(jī)上不會(huì))
NSMutableDictionary *newDataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
NSLog(@"new %@",newDataDic);//打印新數(shù)據(jù)
}
虛擬機(jī)上的打印結(jié)果(bundle和sandbox都修改成功)
真機(jī)上的打印結(jié)果(bundle修改失敗,sandbox修改成功)
注意bool值的修改與一般值修改不一樣,是需要nsnumber作為中間轉(zhuǎn)換
Way NO.3 UserDefaults
plist文件讀取的三種方式還有一種就是使用UserDefaults來(lái)訪問(wèn)一個(gè)特殊的plist文件,使用方法簡(jiǎn)單,不需要文件讀取,使用系統(tǒng)方法,這里就不再介紹UserDefaults了,讀者可自行Google。