蘋果ios開發中的plist檔案的讀寫的例子

來源:互聯網
上載者:User

前言:在做iOS開發時,經常用到plist檔案,  那麼,plist檔案究竟是什麼呢? 在此,我做一個簡單的介紹和使用,它全名是:Property List,屬性列表檔案,它是一種用來儲存序列化後的對象的檔案。屬性列表檔案的副檔名為.plist ,因此通常被稱為 plist檔案,檔案是xml格式。
Plist檔案通常用於儲存使用者佈建,也可以用於儲存捆綁的資訊,同時也可以用來儲存其他的資訊。

下面,我將建立一個項目來學習plist檔案的讀寫。

1、建立項目demo

項目建立之後可以找到項目對應的plist檔案,開啟如下圖所示:

在編輯器中顯示類似與表格的形式,可以在plist上右鍵,用源碼方式開啟,就能看到plist檔案的xml格式了,如下,用sublime text開啟

 

2、建立plist檔案。

File —> New —> New File,或者按command +N快速鍵建立,選擇ios–>Resource下的Property List

 

開啟plistdemo檔案,在空白出右鍵,右鍵選擇Add row 添加資料,添加成功一條資料後,在這條資料上右鍵看到 value Type選擇Dictionary。點加號添加這個Dictionary下的資料:

添加完key之後在後面添加Value的值,添加手機號和年齡

建立完成之後用source code查看到plist檔案是這樣的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://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];
    //讀取plist
    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資料。
寫入的plist檔案會產生在對應程式的沙箱目錄裡。
接著上面讀取plist資料的代碼,加入了寫入資料的代碼,

- (void)viewDidLoad
{
    [super viewDidLoad];
    //讀取plist
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"%@", data);
    //添加一項內容
    [data setObject:@"add some content" forKey:@"c_key"];
    //擷取應用程式沙箱的Documents目錄
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    //得到完整的檔案名稱
    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"test.plist"];
   //輸入寫入
    [data writeToFile:filename atomically:YES];
    //那怎麼證明我的資料寫入了呢?讀出來看看
    NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", data1);
}


在擷取到自己手工建立的plistdemo.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.