您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 屬性列表(Property List)讀寫實作

編輯:iOS 相關資訊

plist 是 iOS 中最簡單直接的資料保存格式,適合用來存放字典與陣列結構的設定資料。以下用實際程式碼說明寫入與讀取。

一、一個重要的觀念:普通物件寫不進 plist

許多人一開始會試著把自訂的 Person 物件放進陣列再 writeToFile:,結果檔案內容空白。原因是 plist 只支援幾種特定的型別:NSNumber、NSString、NSData、NSDate、NSArray、NSDictionary。自訂物件必須先實作 NSCoding 協定並用 NSKeyedArchiver 序列化,才能存入。

另外要注意,writeToFile:atomically: 會重新建立整個檔案,而不是在原檔上追加。因此若要做「讀出→修改→寫回」,必須先讀取現有內容再合併。

二、取得 Documents 目錄路徑

// 第一個參數 NSDocumentDirectory 表示要搜尋 Documents 目錄
// NSUserDomainMask 表示在 App 沙盒中搜尋
// 第三參數 YES 表示展開 ~ 符號
// 在 iOS 平台,此函式回傳的陣列只有一個元素
NSString *documents =
    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];

三、寫入字典

- (void)writeDict {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"JamesWong" forKey:@"name"];
    [dict setObject:[NSNumber numberWithInt:10] forKey:@"age"];

    NSString *home = NSHomeDirectory();
    NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
    NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];

    [dict writeToFile:path atomically:YES];
}

四、寫入陣列

- (void)writeArray {
    NSString *documents =
        [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [documents stringByAppendingPathComponent:@"array.plist"];

    NSArray *array = [NSArray arrayWithObjects:@"1", nil];
    [array writeToFile:path atomically:YES];
}

五、讀取

- (void)readDict {
    NSString *home = NSHomeDirectory();
    NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
    NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@", dict);
}

若檔案不存在,dictionaryWithContentsOfFile: 會回傳 nil,因此使用前應先判空。

六、修改與刪除

因為 plist 沒有「局部更新」API,修改的標準流程是讀出、改記憶體中的物件、再整份寫回:

- (void)deleteDataWithKey:(NSString *)key {
    NSString *path = [[NSSearchPathForDirectoriesInDomains(
        NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
        stringByAppendingPathComponent:@"dict.plist"];

    NSMutableDictionary *root = [[[NSMutableDictionary alloc]
        initWithContentsOfFile:path] mutableCopy];
    NSMutableDictionary *student = [root objectForKey:@"student"];
    [student removeObjectForKey:key];
    [root setValue:student forKey:@"student"];
    [root writeToFile:path atomically:YES];
}

七、更推薦的 NSUserDefaults

如果只是存放少量設定(開關、上次登入帳號、用戶偏好),直接用 NSUserDefaults 會更省事,因為它會自動處理記憶體快取與落盤時機:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:@"JamesWong" forKey:@"name"];
[ud synchronize];

NSString *name = [ud objectForKey:@"name"];

什麼時候該用 plist、什麼時候該用 Core Data 或資料庫?判斷依據是資料量與結構複雜度:幾十筆以內的設定用 NSUserDefaults 或單一 plist;有關聯、需要查詢與排序的大量資料,就該上 Core Data 或 SQLite。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved