2010-10-25 74 views
0

我需要從plist數組中提取數據並將其放入NSArray中。但它似乎沒有工作。從plist數組創建NSArray

這裏是我的主文件:

NSString *path = [[NSBundle mainBundle] pathForResource:@"htmlData" ofType:@"plist"]; 
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 

dictionary = tempDictionary; 
[tempDictionary release]; 

NSMutableArray *nameArray = [[NSMutableArray alloc] init]; 
nameArray = [dictionary objectForKey:@"tagName"]; 

self.sitesArray = nameArray; 

[nameArray release]; 

我的plist文件。命名:htmlData.plist

<plist version="1.0"> 
<dict> 
    <key>tagName</key> 
     <array> 
      <string>&lt;html&gt;</string> 
      <string>&lt;body&gt;</string> 
     </array> 
</dict> 
</plist> 

應該設置self.sitesArray等於@"<html>", @"<body>, nil;,但它不工作。

回答

5

首先,瞭解dictionarytempDictionary指向同一個對象,並且在您釋放它之後無法訪問該對象。所以當你做[tempDictionary release]時,字典被釋放。它不能通過dictionary pointe訪問。

對於此代碼,請嘗試:

NSString *path = [[NSBundle mainBundle] pathForResource:@"htmlData" ofType:@"plist"]; 
NSDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 
self.sitesArray = [dictionary objectForKey:@"tagName"]; 
[dictionary release]; 

您的sitesArray制定者應當保留陣列,或調用[dictionary release]將其釋放。

+0

和下一個內存管理錯誤只在線路上......但在此之後它是兩行 – 2010-10-25 21:28:11

+0

它現在工作,現在我刪除它!謝謝! – Jakir00 2010-10-25 21:28:30