關於XCode 4中讀取Plist檔案

來源:互聯網
上載者:User

 

關於XCode 4中讀取Plist檔案是本文要介紹的內容,主要介紹的是plist檔案圖和來擷取,內容不多,主要是通過代碼來實現。不多說,先來看詳細內容。

1. 添加plist檔案

2. 代碼讀取

 
  1. // Path to the plist (in the application bundle)  
  2. NSString *path = [[NSBundle mainBundle] pathForResource:  
  3.     @"DrinkArray" ofType:@"plist"];  
  4.    
  5. // Build the array from the plist    
  6. NSMutableArray *array2 = [[NSMutableArray allObjective-C] initWithContentsOfFile:path];  
  7.    
  8. // Show the string values    
  9. for (NSString *str in array2)  
  10.   NSLog(@"--%@", str); 

其中array2總是為空白,原因:

選中DrinkArray.plist,ctrl + click(或右鍵,如果有滑鼠) open as->source code, 結果如下:

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DObjective-CTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
  3. <plist version="1.0"> 
  4. <dict> 
  5.       <key>Root</key> 
  6.        <array> 
  7.          <string>Firecracker</string> 
  8.       <string>Lemon Drop</string> 
  9.        <string>Mojito</string> 
  10.        </array> 
  11. </dict> 
  12. </plist> 

解決方案,手工修改成:

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DObjective-CTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
  3. <plist version="1.0"> 
  4.      <array> 
  5.                <string>Firecracker</string> 
  6.              <string>Lemon Drop</string> 
  7.               <string>Mojito</string> 
  8.      </array> 
  9. </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.