2012-05-15 70 views
0

我創建了一個plist文件,其中包含一個包含另外四個數組的單個數組。每個數組包含四個字典。我已經很多嘗試從plist文件中讀取數據。但不能讓我的plist文件的data.the代碼是在這裏無法讀取plist文件,該文件包含數組數組並且有DIctionaries

<plist version="1.0"> 
<dict> 
<key>Root</key> 
<array> 
    <array> 
     <dict> 
      <key>name</key> 
      <string>Czech Republic</string> 
      <key>imagename</key> 
      <string>czechrepublic.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Greece</string> 
      <key>imagename</key> 
      <string>greece.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Poland</string> 
      <key>imagename</key> 
      <string>poland.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Russia</string> 
      <key>imagename</key> 
      <string>russia.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
    </array> 
    <array> 
     <dict> 
      <key>name</key> 
      <string>Denmark</string> 
      <key>imagename</key> 
      <string>denmark.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Germany</string> 
      <key>imagename</key> 
      <string>germany.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Netherlands</string> 
      <key>imagename</key> 
      <string>netherlands.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Portugal</string> 
      <key>imagename</key> 
      <string>portugal.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
    </array> 
    <array> 
     <dict> 
      <key>name</key> 
      <string>Croatia</string> 
      <key>imagename</key> 
      <string>croatia.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Italy</string> 
      <key>imagename</key> 
      <string>italy.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Republic of Ireland</string> 
      <key>imagename</key> 
      <string>ireland.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Spain</string> 
      <key>imagename</key> 
      <string>spain.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
    </array> 
    <array> 
     <dict> 
      <key>name</key> 
      <string>England</string> 
      <key>imagename</key> 
      <string>england.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>France</string> 
      <key>imagename</key> 
      <string>france.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Sweden</string> 
      <key>imagename</key> 
      <string>sweden.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Ukraine</string> 
      <key>imagename</key> 
      <string>ukraine.png</string> 
      <key>url</key> 
      <string>www.google.com</string> 
     </dict> 
    </array> 
    </array> 
</dict> 
</plist> 

,並用於從此plist中讀取的代碼是

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
// get documents path 
NSString *documentsPath = [paths objectAtIndex:0]; 
// get the path to our Data/plist file 
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 

// check to see if Data.plist exists in documents 
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
{ 
    // if not in documents, get property list from main bundle 
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
} 

NSMutableDictionary *dict = [NSMutableDictionary alloc] initWithContentsOfFile:plistPath; 
NSMutableArray *array = [dict objectForKey:@"Root"]; 

數組爲空。

+0

後的錯誤,我們不能讀心術。 – rckoenes

+0

錯誤是字典爲空 –

+0

數組爲空或數組爲零? –

回答

1

具有u嘗試這樣的代碼:

NSString *path = [[NSBundle mainBundle] pathForResource:@」your_Plist_name」 ofType:@」plist」]; 
NSMutableDictionary *saved = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 
NSArray *arr = [saved objectForKey:@"root"]; 

for(int i =0;i<4;i++) 
{ 
    NSArray *next = [arr objectAtIndex:i]; 
    for(int j=0;j<4;j++) 
    { 
     NSDictionary *dict = [next objectAtIndex:j]; 

     NSString *value = [dict objectForKey:@"imagename"]; 
    } 
} 

試試這個代碼,並檢查它是否適合你..

+0

thanx您節省我的時間。但我學到了很多東西。 –

+0

我們歡迎.. :) –

2

您的plist包含一個包含數組的字典,該數組包含四個數組,並且該數組包含字典。

<plist version="1.0"> 
<dict> 
<key>Root</key> 

這些行指定plist是字典而不是數組。


編輯

NSMutableDictionary *dict = [NSMutableDictionary alloc] initWithContentsOfFile:plistPath; 
NSMutableArray *array = [temp objectForKey:@"Root"]; 

爲什麼您使用temp在這裏使用dict

+0

和Caleb說你缺少一些標籤.... –

+0

我編輯了問題 –

+0

發佈您的代碼也... –

1

你錯過關閉</dict></plist>標籤。

+0

我編輯了問題 –

+0

現在我已發佈整個代碼 –

+0

thanx爲您的幫助 –