iOS 應用數據存儲方式(XML屬性列表-plist)

iOS 應用數據存儲方式(XML屬性列表-plist)ios

1、ios應用經常使用的數據存儲方式數據庫

1.plist(XML屬性列表歸檔)
2.偏好設置
3.NSKeydeArchiver歸檔(存儲自定義對象)
4.SQLite3(數據庫,關係型數據庫,不能直接存儲對象,要編寫一些數據庫的語句,將對象拆開存儲)
5.Core Data(對象型的數據庫,把內部環節屏蔽)
 
2、應用沙盒

每一個iOS應用都有本身的應用沙盒(應用沙盒就是文件系統目錄),與其餘文件系統隔離。應用必須待在本身的沙盒裏,其餘應用不能訪問該沙盒(提示:在IOS8中已經開放訪問)app

應用沙盒的文件系統目錄,以下圖所示(假設應用的名稱叫Layer)函數

模擬器應用用沙盒的根路徑在: (apple是用戶名, 7.0是模擬器版本) /Users/apple/Library/Application Support/iPhone Simulator/7.0/Applications atom

3、應用沙盒結構分析操作系統

應用程序包:(上圖中的Layer)包含了全部的資源文件和可執行文件code

Documents:保存應用運行時生成的須要持久化的數據,iTunes同步設備時會備份該目錄。例如,遊戲應用可將遊戲存檔保存在該目錄對象

tmp:保存應用運行時所需的臨時數據,使用完畢後再將相應的文件從該目錄刪除。應用沒有運行時,系統也可能會清除該目錄下的文件。iTunes同步設備時 不會備份該目錄blog

Library/Caches:保存應用運行時生成的須要持久化的數據,iTunes同步設備時不會備份該目錄。通常存儲體積大、不須要備份的非重要數據遊戲

Library/Preference:保存應用的全部偏好設置,iOS的Settings(設置) 應用會在該目錄中查找應用的設置信息。iTunes同步設備時會備份該目錄 

4、應用沙盒常見的獲取方式

● 沙盒根目錄:NSString *home = NSHomeDirectory(); 

● Documents:(2種訪問方式)

 (1)利用沙盒根目錄拼接 」Documents」 字符串
        NSString *home = NSHomeDirectory();
        NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; // 不建議採用,由於新版本的操做系統可能會修改目錄名

 (2)利用 NSSearchPathForDirectoriesInDomains 函數
       // NSUserDomainMask 表明從用戶文件夾下找
       // YES 表明展開路徑中的波浪字符「~」
       NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO); // 在iOS中,只有一個目錄跟傳入的參數匹配,因此這個集合裏面只有一個元素

       NSString *documents = [array objectAtIndex:0]; 

● tmp:NSString *tmp = NSTemporaryDirectory();

● Library/Caches:(跟Documents相似的2種訪問方法)

   (1) 利用沙盒根目錄拼接」Caches」字符串

   (2) 利用 NSSearchPathForDirectoriesInDomains 函數(將函數的第2個參數改 爲:NSCachesDirectory便可)

● Library/Preference:經過 NSUserDefaults 類存取該目錄下的設置信息 

相應的代碼:

 1 #import "NJViewController.h"
 2 #import "NJPerson.h"
 3 
 4 @interface NJViewController ()
 5 - (IBAction)saveDataBtnClick:(id)sender;
 6 - (IBAction)readDataBtnClick:(id)sender;
 7 
 8 @end
 9 
10 @implementation NJViewController
11 /**
12  *   點擊保存按鈕
13  */
14 - (IBAction)saveDataBtnClick:(id)sender {
15     
16     // youtube作法
17 //    NSString *path = @"/Users/apple/Library/Application Support/iPhone Simulator/7.1/Applications/A6D53E11-DDF0-4392-B2D4-FE77A96888A6/Documents/abc.plist";
18     
19     // 獲取應用程序根目錄
20     NSString *home = NSHomeDirectory();
21     
22     // 不建議寫/
23     //NSString *path = [home stringByAppendingString:@"/Documents"];
24     // 不建議 Documents 寫死
25     //NSString *path = [home stringByAppendingPathComponent:@"Documents"];
26     
27     // NSUserDomainMask 在用戶目錄下查找
28     // YES 表明用戶目錄的~
29     // NSDocumentDirectory 查找Documents文件夾
30     // 建議使用以下方法動態獲取
31     NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
32     // 拼接文件路徑
33     NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"];
34     NSLog(@"%@", path);
35     
36     
37     //NSArray *arr = @[@"lnj", @"28"];
38     //[arr writeToFile:path atomically:YES];
39     
40     // NSDictionary *dict = @{@"name": @"lnj", @"age":@"28"};
41     // 調用writeToFile將數據寫入文件
42     // [dict writeToFile:path atomically:YES];
43     
44     /*
45      plist只能存儲系統自帶的一些常規的類, 也就是有writeToFile方法的對象纔可使用plist保存數據
46      字符串/字典/數據/NSNumber/NSData ...
47      */
48     
49     // 自定義的對象不能保存到plist中
50     NJPerson *p = [[NJPerson alloc] init];
51     p.name =@"lnj";
52     
53     NSDictionary *dict = @{@"person": @"abc"};
54     [dict writeToFile:path atomically:YES];
55 }
56 /**
57  *   點擊讀取按鈕
58  */
59 - (IBAction)readDataBtnClick:(id)sender {
60     NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
61     
62     NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"]
63     ;
64     // 讀取數據
65     NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
66     NSLog(@"%@", dict);
67 }
68 @end

5、屬性列表

● 屬性列表是一種XML格式的文件,拓展名爲plist

● 若是對象是NSString、NSDictionary、NSArray、NSData、 NSNumber 等類型,就可使用writeToFile:atomically:用法 直接將對象寫到屬性列表文件中 

相關文章
相關標籤/搜索