)iOS學習之 plist檔案的讀寫,iosplist

來源:互聯網
上載者:User

(轉)iOS學習之 plist檔案的讀寫,iosplist

 在做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檔案是這樣的:[cpp] view plaincopy    <?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檔案的資料現在檔案建立成功了,如何讀取呢,實現代碼如下:[cpp] view plaincopy    - (void)viewDidLoad      {          [super viewDidLoad];          //讀取plist                NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];          NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];          NSLog(@"%@", data);//直接列印資料。      }  列印出來的結果:[cpp] view plaincopy    PlistDemo[6822:f803] {          jack =     {              age = 22;              "phone_num" = 13801111111;          };          tom =     {              age = 36;              "phone_num" = 13901111111;          };      }  這樣就把資料讀取出來了。4、建立和寫入plist檔案在開發過程中,有時候需要把程式的一些配置儲存下來,或者遊戲資料等等。 這時候需要寫入Plist資料。寫入的plist檔案會產生在對應程式的沙箱目錄裡。接著上面讀取plist資料的代碼,加入了寫入資料的代碼,[cpp] view plaincopy    <strong>- (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.      }      </strong>  在擷取到自己手工建立的plistdemo.plist資料後,在這些資料後面加了一項內容,證明輸入寫入了。怎麼證明添加的內容寫入了呢?下面是列印結果:代碼地址:https://github.com/schelling/YcDemo/tree/master/PlistDemo

 

相關文章

聯繫我們

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