2011-03-03 32 views
0

嗨, 我有這樣的plist:從陣列(的plist)可可觸摸刪除字典元素

<plist version="1.0"> 
    <dict> 
     <key>Rows</key> 
     <array> 
      <dict> 
       <key>FavTitle</key> 
       <string>Book1</string> 
       <key>Favourited</key> 
       <string>duh</string> 
       <key>SaveName</key> 
       <string>book1.pdf</string> 
      </dict> 
      <dict> 
       <key>FavTitle</key> 
       <string>Book2</string> 
       <key>Favourited</key> 
       <string>duh</string> 
       <key>SaveName</key> 
       <string>book2.pdf</string> 
      </dict> 
     </array> 
    </dict> 
    </plist> 

其中填充我的UITableView像這樣:

cell.text = [dictionary objectForKey:@"FavTitle"]; 

(是啊是啊,多數民衆贊成貶值)和當選擇一個單元格時,我的字典對象被推送到我的詳細視圖(例如設置Title和UIWebView url),如下所示:

NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 
     DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]]; 
     dvController.selectedSaveName = [dictionary objectForKey:@"SaveName"]; 
     dvController.selectedFavTitle = [dictionary objectForKey:@"FavTitle"]; 
     dvController.selectedFavourited = [dictionary objectForKey:@"Favourited"]; 
     [dvController setHidesBottomBarWhenPushed:YES]; 

     [self.navigationController pushViewController:dvController animated:YES]; 


     [dvController release]; 

這就是所有罰款和花花公子,我可以添加到該數組,但我如何從當前加載的字典呢?這是行不通的:

NSString *documentsDirectory = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents/"]]; 
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"]; 
    NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath]; 
    NSMutableDictionary *thisFav = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"duh", @"Favourited", 
            selectedSaveName, @"SaveName", 
            selectedFavTitle, @"FavTitle", 
            nil]; 
    [[rootDict objectForKey:@"Rows"] removeObject:thisFav];  
    [rootDict writeToFile:writablePath atomically: YES]; 

如何刪除包含字符串'Book2'的條目?我發佈的最後一個片段不起作用!多謝你們!

+0

說明:您想從plist中刪除字典嗎?例如。您必須輸入:Book1和Book2,並且您想要刪除Book2。否則,這個問題根本不明確。 – TechZen 2011-03-03 20:09:46

+0

多孔莫利你好!現在編輯它,抱歉缺乏澄清,謝謝指出那個人! – DexCurl 2011-03-03 20:16:44

回答

0

你不說什麼錯誤,如果有的話,你正在得到。然而你的問題可能與可變性有關。

當你在閱讀的plist使用initWithContentsOfFile只有頂級的字典本身是可變的NSMutableDictionary,任何嵌套的容器是不可變的 - 你的情況Rows陣列將是不可改變的。

爲了使整個事情可變的,你需要使用propertyListFromData:mutabilityOption:format:errorDescription:NSPropertyListSerializationNSPropertyListMutableContainersNSPropertyListMutableContainersAndLeaves傳遞作爲可變性選項。

+0

我不認爲問題在於它的可變性,而在於它在做什麼,我認爲我只能創建一個新對象,然後刪除新創建的對象,並將其移出到NSMutableDictionary中! – DexCurl 2011-03-03 22:00:25

+0

我糾正,不應該相信文檔 - 嵌套數組是可寫的。只要運行它的代碼*就會刪除條目。所以你還有其他問題。什麼'writeToFile:'返回,是或否? – CRD 2011-03-03 23:13:41