iOS開發記錄——Plist使用

前言

在iOS開發過程中,我們會經常用到數據持久化問題,作為數據持久化解決方案之一,plist的使用是一個很方便快捷的方案。

plist保存的地方

1,工程沙盒里(就是程序user Document文件夾下,以讀取文件,寫入文件方式)
2,工程自身里(就是在工程里手動創建一個如.plist文件,把固定的內容寫入,這個需要人工手動寫入)
3,工程沙盒里(保存到user Document下,不過不需要讀寫文件,用系統的 NSUserDefaults 可以快速保存添加讀取刪除基本數據類型,類似于android里的Sharedpreferences )

plist是什么?

它全名是:Property List,屬性列表文件,它是一種用來存儲串行化后的對象的文件。屬性列表文件的擴展名為.plist ,因此通常被稱為 plist文件。文件是xml格式的。Plist文件通常用于儲存用戶設置,也可以用于存儲捆綁的信息。

如何使用plist——plist的讀取,修改和刪除

plist的讀?。ㄊ褂迷诠こ套陨砝锏姆绞剑?/h5>
  1. 創建一項測試project
    1.png
  2. 創建一個plist文件


    2.png

    3.png

    在plist中添加一些信息


    4.png
    plist可以支持很多類型數據包括字典和數組
    5.png
  3. 讀取代碼
- (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);//直接打印數據
}

打印結果

6.png
plist的修改寫入

(之前有說,保存在工程自身的plist并不能修改寫入,所以這里需要通過沙盒路徑創建plist并修改保存,還有一個坑,模擬器與真機權限可能不一致,在模擬機上能夠通過nsbundle路徑修改成功,但是真機上并不能,所以建議需要修改的plist都使用沙盒路徑來新建和修改并且兩者所獲取的plist并不是同一個文件,代碼解釋如下)

- (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"];//沒有會自動創建
    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);//直接打印數據
    
    
    //工程自身的plist  
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"PropertyListTest" ofType:@"plist"];
    NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    NSLog(@"nsbundle %@",dataDic);//直接打印數據
    
}
- (void)writeDataToPlist{
    //這里使用位于沙盒的plist(程序會自動新建的那一個)
    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(手動新建的那一個)
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"PropertyListTest" ofType:@"plist"];
    NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    
    //開始修改及寫入
    NSNumber *number = @10;
    dataDic[@"1"] = number;//修改
    NSNumber *boolNumber = [NSNumber numberWithBool:YES];//bool值只能通過nsnumber修改
    dataDic[@"3"] = boolNumber;
    [dataDic writeToFile:plistPath atomically:YES];
    
    //重新獲取數據 看是否有變動(虛擬機上會有變動,但是真機上不會)
    NSMutableDictionary *newDataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    NSLog(@"new %@",newDataDic);//打印新數據
}

虛擬機上的打印結果(bundle和sandbox都修改成功)


虛擬機測試結果

真機上的打印結果(bundle修改失敗,sandbox修改成功)

真機測試結果
注意bool值的修改與一般值修改不一樣,是需要nsnumber作為中間轉換

Way NO.3 UserDefaults

plist文件讀取的三種方式還有一種就是使用UserDefaults來訪問一個特殊的plist文件,使用方法簡單,不需要文件讀取,使用系統方法,這里就不再介紹UserDefaults了,讀者可自行Google。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容