开发者

Find dictionary in NSMutableDictionary and remove it

    <plist version="1.0">
<dict>
    <key>Rows</key>
    <array>
        <dict>
            <key>FavTitle</key>
            <string>AppliedVersion</string>
            <key>SaveName</key>
            <string>122.pdf</string>
            <key>duh</key>
            <string>Favourited</string>
        </dict>
        <dict>
            <key>FavTitle</key>
            <string>Test Eng. Version</string>
            <key>SaveName</key>
            <string>dsds.pdf</string>
            <key>duh</key>
            <string>Favourited</string>
        </dict>
    </array>
</dict>
</plist>

Trying and failing to remove things with this:

NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"];
    NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath];
    NSMutableDictionary *newFav = [NSMutableDictionary dictionaryWithObjectsAndKeys: selectedFavTitle, @"FavTitle", @"Favourited", @"duh", selectedSaveName, @"SaveName", nil];
    [[rootDict object开发者_开发问答ForKey:@"Rows"] removeObjectIdenticalTo:newFav];  
    [rootDict writeToFile:writablePath atomically: YES];

Its been doing my head in! The flaw is with:

[[rootDict objectForKey:@"Rows"] removeObjectIdenticalTo:newFav]; 

as that does not remove the matched object


NSMutableDictionary *newFav = [NSMutableDictionary dictionaryWithObjectsAndKeys: selectedFavTitle, @"FavTitle", @"Favourited", @"duh", selectedSaveName, @"SaveName", nil];

This line creates a new instance of NSMutableDictionary. So when you call removeObjectIdenticalTo:, it doesn't match anything because that new instance isn't the same object as the one in rootDict.

You can either filter out the objects you don't want with filterUsingPredicate: or you can loop over rootDict manually to find and remove the object manually.


My guess would be encoding problems. You didn't tell us which encoding you specified when you saved this plist file and you didn't specify one explicitly like this:

<?xml version="1.0" encoding="UTF-8"?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜