您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發(92)之 plist 檔的讀寫

編輯:iOS 開發綜合

做 iOS 開發時經常會用到 plist 檔。它的全名是 Property List(屬性列表),是一種用來儲存序列化物件的檔案,副檔名為 .plist,內容為 XML 格式。plist 通常用來儲存使用者設定,也可用來存放打包時的靜態資訊。

1、建立專案

新增一個專案 PlistDemo,建立完成後可在專案中找到對應的 Info.plist,打開後以表格形式顯示:

在檔案上按右鍵選擇「Open As > Source Code」,就能看到它的 XML 結構。

2、建立 plist 檔

按 Command + N,或 File > New > New File,選擇 macOS 下的 Property List,命名為 plistdemo:

打開檔案後在空白处按右鍵選擇 Add Row 新增一筆資料,在該列上再按右鍵把 Value Type 設為 Dictionary,然後點加號加入這個 Dictionary 底下的鍵值。

以下示範建立两个人物,各自擁有電話與年齡:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>jack</key>
    <dict>
        <key>phone_num</key>
        <string>13801111111</string>
        <key>age</key>
        <string>22</string>
    </dict>
    <key>tom</key>
    <dict>
        <key>phone_num</key>
        <string>13901111111</string>
        <key>age</key>
        <string>36</string>
    </dict>
</dict>
</plist>

3、讀取 plist 的資料

檔案建立完成後,讀取只需要兩行:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"%@", data);
}

輸出結果:

PlistDemo[6822:f803] {
    jack = {
        age = 22;
        "phone_num" = 13801111111;
    };
    tom = {
        age = 36;
        "phone_num" = 13901111111;
    };
}

4、建立並寫入 plist 檔

開發過程中常需要把程式的設定或遊戲存檔保存下來,這時就要寫入 plist。寫入的檔案會產生在 App 的沙盒目錄中:

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1. 讀取 bundle 中的 plist
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"%@", data);

    // 2. 新增一筆內容
    [data setObject:@"add some content" forKey:@"c_key"];

    // 3. 取得沙盒的 Documents 目錄
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [paths objectAtIndex:0];
    NSString *filename = [docPath stringByAppendingPathComponent:@"test.plist"];

    // 4. 寫入檔案
    [data writeToFile:filename atomically:YES];

    // 5. 讀回來驗證
    NSMutableDictionary *verify = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", verify);
}

幾個實務重點

bundle 是唯讀的。上面第 1 步讀的是 App bundle 內的 plist,那是打包時就固定的預設值,程式無法修改。要寫入就必須像第 3 步那樣,把檔案寫到 Documents 或 Library 等可寫目錄。

plist 只支援有限型別。能寫入的只有 NSString、NSNumber、NSData、NSDate、Boolean、NSArray、NSDictionary。自訂物件必須先轉為這些型別(例如用 NSKeyedArchiver 轉成 NSData)才能存入。

writeToFile 的回傳值要檢查。寫入失敗最常見的原因就是型別不支援,或路徑不可寫:

if ([data writeToFile:filename atomically:YES]) {
    NSLog(@"寫入成功");
} else {
    NSLog(@"寫入失敗");
}

哪裡該放資料?使用者可修改的設定值放在 Documents(會被 iCloud/iTunes 備份);可重新產生的快取放在 Library/Caches;純粹的偏好設定則建議直接用 NSUserDefaults,它本質上也是 plist,只是幫你處理好了路徑與讀寫時機。

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