简体   繁体   English

从数组中删除字典元素(plist)Cocoa Touch

[英]Delete dictionary element from array (plist) Cocoa Touch

Hey guys, I have this 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>

which populates my UITableView like so: 像这样填充我的UITableView:

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

(yeah yeah thats depreciated) and when a cell is selected, my dictionary objects are pushed to my detail view (eg to set Title and UIWebView url) like so: (是的,是折旧的),当选择一个单元格时,我的字典对象将被推送到我的详细信息视图(例如,设置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];

Thats all fine and dandy and I can add to that array, but how do I take the current loaded dictionary from it? 一切都很好,我可以添加到该数组中,但是如何从中获取当前已加载的字典? This isn't working: 这不起作用:

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];

How could I remove the entry that has the string 'Book2'? 如何删除具有字符串“ Book2”的条目? The last snippet I've posted doesn't work! 我发布的最后一个片段无效! Thanks guys! 多谢你们!

You don't say what error, if any, you are getting. 您不会说出什么错误,如果有的话。 However your problem might be with mutability. 但是,您的问题可能与可变性有关。

When you read in a plist to an NSMutableDictionary using initWithContentsOfFile only the top-level dictionary itself is mutable, any nested containers are immutable - in your case the Rows array would be immutable. 当您使用initWithContentsOfFile读取plist到NSMutableDictionary只有顶级字典本身是可变的,任何嵌套容器都是不可变的-在您的情况下, Rows数组将是不可变的。

To make the whole thing mutable you need to use propertyListFromData:mutabilityOption:format:errorDescription: from NSPropertyListSerialization passing in NSPropertyListMutableContainers or NSPropertyListMutableContainersAndLeaves as the mutability option. 为了使整个事情可变的,你需要使用propertyListFromData:mutabilityOption:format:errorDescription:NSPropertyListSerialization顺便NSPropertyListMutableContainersNSPropertyListMutableContainersAndLeaves的可变性选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM