iOS 應用資料存放區方式(XML屬性列表-plist)

來源:互聯網
上載者:User

標籤:

iOS 應用資料存放區方式(XML屬性列表-plist)

一、ios應用常用的資料存放區方式

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

每個iOS應用都有?己的應?沙箱(應用沙箱就是檔案系統目錄),與其他檔案系統隔離。應?必須待在?己的沙箱裡,其他應用不能訪問該沙箱(提示:在IOS8中已經開放訪問)

應?沙箱的檔案系統?錄,如所示(假設應用的名稱叫Layer)

模擬器應?用沙箱的根路徑在: (apple是?使用者名稱, 7.0是模擬器版本) /Users/apple/Library/Application Support/iPhone Simulator/7.0/Applications 

三、應用沙箱結構分析

應?程式包:(中的Layer)包含了所有的資源檔和可執行檔

Documents:儲存應?運行時產生的需要持久化的資料,iTunes同步裝置時會備份該目錄。例如,遊戲應用可將遊戲存檔儲存在該目錄

tmp:儲存應?運行時所需的臨時資料,使?完畢後再將相應的檔案從該目錄刪除。應用沒有運行時,系統也可能會清除該目錄下的檔案。iTunes同步裝置時 不會備份該目錄

Library/Caches:儲存應用運行時?成的需要持久化的資料,iTunes同步裝置時不會備份該目錄。?一般儲存體積大、不需要備份的非重要資料

Library/Preference:儲存應用的所有喜好設定,iOS的Settings(設定) 應?會在該?錄中尋找應?的設定資訊。iTunes同步裝置時會備份該目錄 

四、應用沙箱常見的擷取方式

● 沙箱根目錄:NSString *home = NSHomeDirectory(); ● Documents:(2種?方式)

● 利用沙箱根目錄拼接”Documents”字串
NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; // 不建議採用,因為新版本的作業系統可能會修改目錄名

● 利?NSSearchPathForDirectoriesInDomains函數
// NSUserDomainMask 代表從使用者檔案夾下找
// YES 代表展開路徑中的波浪字元“~”
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO); // 在iOS中,只有一個目錄跟傳入的參數匹配,所以這個集合裡面只有一個元素

NSString *documents = [array objectAtIndex:0]; 

● tmp:NSString *tmp = NSTemporaryDirectory();

● Library/Caches:(跟Documents類似的2種?方法)

● 利用沙箱根目錄拼接”Caches”字串

● 利?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 NJViewController11 /**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 [email protected]"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

五、屬性列表

● 屬性列表是一種XML格式的檔案,拓展名為plist

● 如果對象是NSString、NSDictionary、NSArray、NSData、 NSNumber等類型,就可以使用writeToFile:atomically:?法 直接將對象寫到屬性列表檔案中 

 

iOS 應用資料存放區方式(XML屬性列表-plist)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.