iOS數據持久化存儲之屬性列表

 屬性列表(plist)

  iOS提供了一種plist格式的文件(屬性列表)用於存儲輕量級的數據,屬性列表是一種XML格式的文件,拓展名爲plist。若是對象是NSString、NSDictionary、NSArray、NSData、 NSNumber等類型,就可使用writeToFile:atomically:⽅法 直接將對象寫到屬性列表文件中該格式保存的數據能夠直接使用NSDictionary和NSArray讀取 css

(一)、使用NSUserDefault 實現持久化數組

     下面來看下 NSUserDefault 本地保存的位置,數據持久化之沙盒目錄有說起。Library/Preferences 這個目錄下的 plist 文件就是其保存的目錄。
      NSUserDefault 的用法,主要是保存和讀取ui

      初始化一個 NSUserDefaultatom

     + (NSUserDefaults *)standardUserDefaults;url

     設置數據的方法spa

      - (void)setObject:(nullable id)value forKey:(NSString *)defaultName;     code

      - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;   對象

      - (void)setFloat:(float)value forKey:(NSString *)defaultName;圖片

      - (void)setDouble:(double)value forKey:(NSString *)defaultName;rem

      - (void)setBool:(BOOL)value forKey:(NSString *)defaultName;

      - (void)setURL:(nullable NSURL *)url forKey:(NSString *)defaultName NS_AVAILABLE(10_6, 4_0);

     讀取數據的方法:

      - (nullable id)objectForKey:(NSString *)defaultName; 

      - (nullable NSString *)stringForKey:(NSString *)defaultName;

      - (nullable NSArray *)arrayForKey:(NSString *)defaultName;

      - (nullable NSDictionary<NSString *, id> *)dictionaryForKey:(NSString *)defaultName;

      - (nullable NSData *)dataForKey:(NSString *)defaultName;

      - (nullable NSArray<NSString *> *)stringArrayForKey:(NSString *)defaultName;

      - (NSInteger)integerForKey:(NSString *)defaultName;

      - (float)floatForKey:(NSString *)defaultName;

      - (double)doubleForKey:(NSString *)defaultName;

      - (BOOL)boolForKey:(NSString *)defaultName;

      - (nullable NSURL *)URLForKey:(NSString *)defaultName NS_AVAILABLE(10_6, 4_0);

      刪除數據的方法:

       - (void)removeObjectForKey:(NSString *)defaultName;

      保存數據:

     // 若是不手動調用,系統會自動保存,但時間不定

       - (BOOL)synchronize;    

    使用方法

    //1.得到NSUserDefaults文件

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    //2.向文件中寫入內容

    [userDefaults setObject:@"AAA" forKey:@"a"];

    [userDefaults setBool:YES forKey:@"sex"];

    [userDefaults setInteger:21 forKey:@"age"];

    //2.1當即同步

    [userDefaults synchronize];

    //3.讀取文件

    NSString *name = [userDefaults objectForKey:@"a"];

    BOOL sex = [userDefaults boolForKey:@"sex"];

    NSInteger age = [userDefaults integerForKey:@"age"];


   // 存儲id類型數據

     + (void)setValue:(id)value andKey:(NSString *)key

    {

        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

        [userDefaults setObject:value forKey:key];

        [userDefaults synchronize];

    }

// 獲取數據

    + (NSString *)getValueByKey:(NSString *)key

    {

        NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];

        NSString *value = [settings objectForKey:key];

        return value;

     }

   注意:

      偏好設置是專門用來保存應用程序的配置信息的,通常不要在偏好設置中保存其餘數據。
      若是沒有調用synchronize方法,系統會根據I/O狀況不定時刻地保存到文件中。因此若是須要當即寫入文件的就必須調用synchronize方法。
      偏好設置會將全部數據保存到同一個文件中。即preference目錄下的一個以此應用包名來命名的plist文件。

 

 (二)、手動添加plist文件

      新建文件-->Resource-->Property List

       輸入圖片說明

           

    plist文件的根類型只能是NSArray或NSDictionary

     輸入圖片說明

    輸入圖片說明

 

      將plist文件中的數據讀入對應的根類型

    // 一、獲取文件所在的路徑,Resource:文件名稱、Type:文件格式

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"userInfos" ofType:@"plist"];

    // 二、從路徑中獲取對應格式的數據

    // 若是Root爲NSArray,則使用數組保存

    NSArray *infos = [NSArray arrayWithContentsOfFile:filePath];

    NSLog(@"%@",infos);

    // 若是Root爲NSDictionary,則使用字典保存

    NSDictionary *infoDic = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSLog(@"%@",infoDic);

 

 (三)、直接講數據寫入plist文件

      因爲 NSUserDefault 本質上就是經過 plist 文件來實現屬性的持久化。因此,咱們能夠經過本身建立一個 plist 文件來實現屬性的持久化。

        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *docPath = [path objectAtIndex:0];    NSString *myFile = [docPath stringByAppendingPathComponent:@"test.plist"];    NSMutableDictionary *contentDic;    // 判斷本地是否存在 plist 文件    if ([[NSFileManager defaultManager] fileExistsAtPath:myFile] == NO) {    NSFileManager* fm = [NSFileManager defaultManager];     // 建立一個文件    [fm createFileAtPath:myFile contents:nil attributes:nil];    contentDic = [[NSMutableDictionary alloc] init];    } else {     contentDic = [[NSMutableDictionary alloc] initWithContentsOfFile:myFile];    }     // 數據的讀寫操做    [contentDic setObject:@"1234" forKey:@"passWord"];    // 將修改都的數據保存到 plist 文件中    [contentDic writeToFile:myFile atomically:YES];

(四)、總結
    plist文件的讀寫效率比較高,因爲他的讀寫須要將全部的數據取出再所有保存,因此只適合小數據。

相關文章
相關標籤/搜索