Search code examples
iosxmlplist

iOS - Adding items to plist


I'm writing a plist from an 'NSMutableDictionary'. That works ok.

But when I want to add to the file, the new entry overrides the first entry.

What I'd like is something like this:

<?xml version="1.0" encoding="UTF-8"?>
<names>
<name1>
    <key>
        firstname
    </key>
    <value>
        mememememe
    </value>
    <key>
        lastname
    </key>
    <value>
        mememememe
    </value>

</name1>
<name2>
    <key>
        firstname
    </key>
    <value>
        mememememe
    </value>
    <key>
        lastname
    </key>
    <value>
        mememememe
    </value>

    </name2>
</names>
Here's my code so far:



    NSString *filename = [documents stringByAppendingPathComponent:@"faves.plist"];
    NSMutableDictionary *loadedMiscDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filename];

 NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [directories firstObject];

    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
    [tempDict setValue:firstNameString forKey:@"firstname"];
    [tempDict setValue:secondNameString forKey:@"secondName"];

  [loadedMiscDictionary addEntriesFromDictionary:tempDict];
    [loadedMiscDictionary writeToFile:filename atomically:YES];

Please let me know what I'm doing wrong. TIA

UPDATE

I changed my code snippet above..


Solution

  • you should use right click + Add row for top category and for subcategories use + sign to add new one in plist file