iOS資料持久化儲存之屬性列表,ios資料存放區

來源:互聯網
上載者:User

iOS資料持久化儲存之屬性列表,ios資料存放區
 屬性列表(plist)

  iOS提供了一種plist格式的檔案(屬性列表)用於儲存輕量級的資料,並且只能儲存NSDictionary、NSArray、NSString、NSNumber、Boolean、NSData、NSDate 類型的資料。將這些類型的資料儲存為plist格式檔案,該格式儲存的資料可以直接使用NSDictionary和NSArray讀取 

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

     下面來看下 NSUserDefault 本地儲存的位置,資料持久化之沙箱目錄有提及。Library/Preferences 這個目錄下的 plist 檔案就是其儲存的目錄。
      NSUserDefault 的用法,主要是儲存和讀取

      初始化一個 NSUserDefault

    + (NSUserDefaults *)standardUserDefaults;

     設定資料的方法

    - (void)setObject:(nullable id)value forKey:(NSString *)defaultName;    - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;    - (void)setFloat:(float)value forKey:(NSString *)defaultName;    - (void)setDouble:(double)value forKey:(NSString *)defaultName;    - (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;
 

   // 儲存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;

     }

 

 (二)、手動添加plist檔案

      建立檔案-->Resource-->Property List

       

           

    plist檔案的根類型只能是NSArray或NSDictionary

     

    

 

      將plist檔案中的資料讀入對應的根類型

    // 1、擷取檔案所在的路徑,Resource:檔案名稱、Type:檔案格式

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

    // 2、從路徑中擷取對應格式的資料

    // 如果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檔案的讀寫效率比較高,由於他的讀寫需要將所有的資料取出再全部儲存,所以只適合小資料。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.