原文:http://www.itdecent.cn/p/b37c1bcf8cdc
前言
在iOS開發(fā)過程中,我們會(huì)經(jīng)常用到數(shù)據(jù)持久化問題,作為數(shù)據(jù)持久化解決方案之一,plist的使用是一個(gè)很方便快捷的方案。
plist保存的地方
1,工程沙盒里(就是程序user Document文件夾下,以讀取文件,寫入文件方式)
2,工程自身里(就是在工程里手動(dòng)創(chuàng)建一個(gè)如.plist文件,把固定的內(nèi)容寫入,這個(gè)需要人工手動(dòng)寫入)
3,工程沙盒里(保存到user Document下,不過不需要讀寫文件,用系統(tǒng)的 NSUserDefaults 可以快速保存添加讀取刪除基本數(shù)據(jù)類型,類似于android里的Sharedpreferences )
plist是什么?
它全名是:Property List,屬性列表文件,它是一種用來存儲(chǔ)串行化后的對(duì)象的文件。屬性列表文件的擴(kuò)展名為.plist ,因此通常被稱為 plist文件。文件是xml格式的。Plist文件通常用于儲(chǔ)存用戶設(shè)置,也可以用于存儲(chǔ)捆綁的信息。
如何使用plist——plist的讀取,修改和刪除
plist的讀?。ㄊ褂迷诠こ套陨砝锏姆绞剑?/p>
創(chuàng)建一項(xiàng)測(cè)試project

1.png
創(chuàng)建一個(gè)plist文件

2.png

3.png
在plist中添加一些信息

4.png
plist可以支持很多類型數(shù)據(jù)包括字典和數(shù)組

5.png
讀取代碼
- (void)viewDidLoad {? ? [superviewDidLoad];//讀取plist[selfgetDataFromPlist];}- (void)getDataFromPlist{NSString*plistPath = [[NSBundlemainBundle]pathForResource:@"PropertyListTest"ofType:@"plist"];NSMutableDictionary*dataDic = [[NSMutableDictionaryalloc]initWithContentsOfFile:plistPath];NSLog(@"%@",dataDic);//直接打印數(shù)據(jù)}
打印結(jié)果

6.png
plist的修改寫入
(之前有說,保存在工程自身的plist并不能修改寫入,所以這里需要通過沙盒路徑創(chuàng)建plist并修改保存,還有一個(gè)坑,模擬器與真機(jī)權(quán)限可能不一致,在模擬機(jī)上能夠通過nsbundle路徑修改成功,但是真機(jī)上并不能,所以建議需要修改的plist都使用沙盒路徑來新建和修改并且兩者所獲取的plist并不是同一個(gè)文件,代碼解釋如下)
- (void)viewDidLoad {? ? [superviewDidLoad];//讀取plist[selfgetDataFromPlist];// 寫入plist[selfwriteDataToPlist];}
- (void)getDataFromPlist{//沙盒獲取路徑NSArray*pathArray =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString*path = [pathArray objectAtIndex:0];//獲取文件的完整路徑NSString*filePatch = [path stringByAppendingPathComponent:@"PropertyListTest.plist"];//沒有會(huì)自動(dòng)創(chuàng)建NSLog(@"file patch%@",filePatch);NSMutableDictionary*sandBoxDataDic = [[NSMutableDictionaryalloc]initWithContentsOfFile:filePatch];if(sandBoxDataDic==nil) {? ? ? ? sandBoxDataDic = [NSMutableDictionarynew];? ? ? ? sandBoxDataDic[@"test"] =@"test";? ? ? ? [sandBoxDataDic writeToFile:filePatch atomically:YES];? ? }NSLog(@"sandBox %@",sandBoxDataDic);//直接打印數(shù)據(jù)//工程自身的plist? NSString*plistPath = [[NSBundlemainBundle]pathForResource:@"PropertyListTest"ofType:@"plist"];NSMutableDictionary*dataDic = [[NSMutableDictionaryalloc]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 = [[NSMutableDictionaryalloc]initWithContentsOfFile:filePatch];NSLog(@"old sandBox is %@",sandBoxDataDic);? ? sandBoxDataDic[@"test"] =@"hello world";? ? [sandBoxDataDic writeToFile:filePatch atomically:YES];? ? sandBoxDataDic = [[NSMutableDictionaryalloc]initWithContentsOfFile:filePatch];NSLog(@"new sandBox is %@",sandBoxDataDic);//這里使用的是位于工程自身的plist(手動(dòng)新建的那一個(gè))NSString*plistPath = [[NSBundlemainBundle]pathForResource:@"PropertyListTest"ofType:@"plist"];NSMutableDictionary*dataDic = [[NSMutableDictionaryalloc]initWithContentsOfFile:plistPath];//開始修改及寫入NSNumber*number = @10;? ? dataDic[@"1"] = number;//修改NSNumber*boolNumber = [NSNumbernumberWithBool:YES];//bool值只能通過nsnumber修改dataDic[@"3"] = boolNumber;? ? [dataDic writeToFile:plistPath atomically:YES];//重新獲取數(shù)據(jù) 看是否有變動(dòng)(虛擬機(jī)上會(huì)有變動(dòng),但是真機(jī)上不會(huì))NSMutableDictionary*newDataDic = [[NSMutableDictionaryalloc]initWithContentsOfFile:plistPath];NSLog(@"new %@",newDataDic);//打印新數(shù)據(jù)}
虛擬機(jī)上的打印結(jié)果(bundle和sandbox都修改成功)

虛擬機(jī)測(cè)試結(jié)果
真機(jī)上的打印結(jié)果(bundle修改失敗,sandbox修改成功)

真機(jī)測(cè)試結(jié)果
注意bool值的修改與一般值修改不一樣,是需要nsnumber作為中間轉(zhuǎn)換
Way NO.3 UserDefaults
plist文件讀取的三種方式還有一種就是使用UserDefaults來訪問一個(gè)特殊的plist文件,使用方法簡(jiǎn)單,不需要文件讀取,使用系統(tǒng)方法,這里就不再介紹UserDefaults了,讀者可自行Google。