iOS學習之 plist檔案的讀寫

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   strong   

      在做iOS開發時,經經常使用到到plist檔案,  那plist檔案是什麼呢? 它全名是:Property List,屬性列表檔案,它是一種用來儲存序列化後的對象的檔案。屬性列表檔案的副檔名為.plist ,因此通常被稱為 plist檔案。檔案是xml格式的。

Plist檔案通經常使用於儲存使用者佈建,也能夠用於儲存捆綁的資訊


我們建立一個項目來學習plist檔案的讀寫。


1、建立項目Plistdemo


項目建立之後能夠找到項目相應的plist檔案,開啟例如以所看到的:


在編輯器中顯示相似與表格的形式,能夠在plist上右鍵,用原始碼方式開啟,就能看到plist檔案的xml格式了。



2、建立plist檔案。


按command +N快速鍵建立,或者File —> New —> New File,選擇Mac OS X下的Property List



建立plist檔案名稱為plistdemo。



開啟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);        // Do any additional setup after loading the view, typically from a nib.}

在擷取到自己手工建立的plistdemo.plist資料後,在這些資料後面加了一項內容,證明輸入寫入了。

怎麼證明加入的內容寫入了呢?以下是列印結果:



代碼地址:https://github.com/schelling/YcDemo/tree/master/PlistDemo

著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者部落格連結,謝謝!





聯繫我們

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