數(shù)據(jù)存儲(chǔ)

一、應(yīng)用的 bundle和沙盒

1.基本信息:

  • bundle 和 沙盒都指的是兩個(gè)文件目錄
  • bundle 存放的是應(yīng)用的可執(zhí)行文件及其資源圖片等
  • 沙盒目錄可以用來(lái)數(shù)據(jù)存儲(chǔ)和讀取的操作
    注意:一個(gè)應(yīng)用不允許訪問(wèn)其他應(yīng)用的沙盒目錄,只能在自己的沙盒中進(jìn)行操作

2.沙盒的目錄

Document:文檔目錄,用來(lái)存儲(chǔ)一些比較重要的數(shù)據(jù),由應(yīng)用本身產(chǎn)生的數(shù)據(jù),應(yīng)用備份的時(shí)候,會(huì)備份到此文件目錄下。注意 一些網(wǎng)絡(luò)的緩存數(shù)據(jù)不能放到此目錄下,否則,應(yīng)用上架的時(shí)候會(huì)被拒絕
Library:此目錄下有兩個(gè)文件夾,Caches緩存目錄,是用來(lái)保存從網(wǎng)上下載下來(lái)的需要持久化的數(shù)據(jù),應(yīng)用在備份的時(shí)候不會(huì)自動(dòng)備份此目錄,也不會(huì)自動(dòng)刪除此目錄下的數(shù)據(jù),需要程序內(nèi)部提供刪除緩存數(shù)據(jù)的操作。Preferences是用來(lái)保存用戶的常用的數(shù)據(jù),比如用戶在登錄的時(shí)候,可以將用戶名和密碼保存到此目錄下,等到下次登錄的時(shí)候,不用再輸入賬號(hào)密碼,直接就可以自動(dòng)登錄了。
tmp:臨時(shí)文件目錄,用來(lái)保存臨時(shí)文件的,系統(tǒng)會(huì)自動(dòng)的刪除此目錄下的臨時(shí)文件數(shù)據(jù)。


二、幾種數(shù)據(jù)存儲(chǔ)的方式:

  • 1、plist 文件存儲(chǔ)數(shù)據(jù):系統(tǒng)提供的字典數(shù)據(jù)等可以直接通過(guò) writeToFile:的方式寫(xiě)入文件,使用簡(jiǎn)單方便,是偏好設(shè)置存儲(chǔ)的基礎(chǔ)
    寫(xiě)入:
    字典:
 // 1.準(zhǔn)備數(shù)據(jù)!
   NSDictionary *dict = @{
                          @"name" : @"leige",
                          @"age"  : @18,
                          @"xifuer" : @(YES)
                          };
   
   // 2.寫(xiě)入plist文件!
   // 參數(shù)1 文件路徑! -> documents
   // - 獲取doc的路徑
   NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
   // - 拼接文件的名稱
   // stringByAppendingPathComponent -> 在documents的路徑基礎(chǔ)上拼接 /abc.plist!
   NSString *filePath = [docPath stringByAppendingPathComponent:@"/abc.plist"];
   
   NSLog(@"%@", filePath);
   // 參數(shù)2 原子屬性 -> 設(shè)置為YES,必要安全.即使程序崩潰,數(shù)據(jù)寫(xiě)入也ok!
   [dict writeToFile:filePath atomically:YES];

數(shù)組:

// 1.準(zhǔn)備數(shù)據(jù)!
   NSArray *arr = @[
                    @{
                        @"name" : @"jack",
                        @"age" : @"18"
                        },
                    @{
                        @"name" : @"rose",
                        @"age"  : @"21"
                        },
                    @{
                        @"name" : @"gebilaowang",
                        @"age"  : @"38"
                        }
                    ];
   
   // 2.寫(xiě)入plist文件!
   // 參數(shù)1 文件路徑! -> documents
   // - 獲取doc的路徑
   NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
   // - 拼接文件的名稱
   // stringByAppendingPathComponent -> 在documents的路徑基礎(chǔ)上拼接 /abc.plist!
   NSString *filePath = [docPath stringByAppendingPathComponent:@"/abc.plist"];
   
   NSLog(@"%@", filePath);
   // 參數(shù)2 原子屬性 -> 設(shè)置為YES,必要安全.即使程序崩潰,數(shù)據(jù)寫(xiě)入也ok!
   [arr writeToFile:filePath atomically:YES];

讀取:
字典:

   // 1.獲取文件路徑
   NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"abc.plist"];
   
   // 2.加載數(shù)據(jù)
   NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

數(shù)組:

   // 1.獲取文件路徑
   NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"abc.plist"];
 
   // 2.加載數(shù)據(jù)
   NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
  
  • 2、偏好設(shè)置存儲(chǔ)數(shù)據(jù):
    1.這是系統(tǒng)提供的 API,方便高效
    2.程序員不需要管理存儲(chǔ)數(shù)據(jù)的路徑及文件等信息,統(tǒng)一由系統(tǒng)自己負(fù)責(zé)
    3.偏好設(shè)置存儲(chǔ)的本質(zhì)是以字典的形式將數(shù)據(jù)寫(xiě)入到 plist 文件中
    存儲(chǔ)
// 1.準(zhǔn)備數(shù)據(jù)!
    NSInteger font = 16;
    NSString *age = @"20";
    NSString *score = @"120";
    
    // 1.通過(guò)偏好設(shè)置存儲(chǔ)
    // 用戶偏好 -> 單例!可以直接去存儲(chǔ)數(shù)據(jù)!
    // -獲取偏好設(shè)置對(duì)象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // - 存儲(chǔ)信息
    [defaults setInteger:font forKey:@"yy_font"];
    [defaults setObject:age forKey:@"yy_age"];
    [defaults setObject:score forKey:@"yy_score"];
    
    // 同步 -> 保證數(shù)據(jù)立刻寫(xiě)入磁盤(pán)! iOS7以后就不需要寫(xiě)這行代碼! 了解!
//    [defaults synchronize];

讀?。?/strong>

// 1.獲取偏好設(shè)置對(duì)象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    // 2.取出數(shù)據(jù)
    NSInteger font = [defaults integerForKey:@"yy_font"];
    NSString *age = [defaults objectForKey:@"yy_age"];
    NSString *score = [defaults objectForKey:@"yy_score"];

  • 3、歸檔&解檔:
    它主要用于存儲(chǔ)自定義類型的對(duì)象
    用法:
    首先需要遵守 NSCoding 協(xié)議 <NSCoding>
    實(shí)現(xiàn)協(xié)議方法,歸檔時(shí)調(diào)用:
- (void)encodeWithCoder:(NSCoder *)encoder

解檔時(shí)調(diào)用:

- (instancetype)initWithCoder:(NSCoder *)decoder
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容