Appending data to PLIST - iPhone

2019-01-27 00:33发布

we need to be able to read in the contents of an existing plist file then add to the array / dictionary then write this new data to the plist file. The stricture is simple. We want multiple root keys as arrays and each with a few values. The structure looks like this,

124818240124810284.JPG          Array
                item 0          String     Some Attribute 1
                item 1          String     Some Attribute 2

So far we can create the above with no issue. But, as soon as we go to write another array to the plist the file is simply overwritten and all current contents are lost. What we need to do is read in the above and add to it so we get something like this,

124818240124810284.JPG          Array
                item 0          String     Some Attribute 1
                item 1          String     Some Attribute 2
354273577823597297.JPG          Array
                item 0          String     Some Attribute 1
                item 1          String     Some Attribute 2

And so on. We're at a loss and have been struggling with this for a day now. Please help where you can! Thanks in advance!!

We are currently writing this file as follows

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Gallery.plist"];

// set the variables to the values in the text fields
NSMutableArray *myPhotos = [[NSMutableArray alloc] initWithCapacity:2];
[myPhotos addObject:@"NO"];
[myPhotos addObject:@"NO"];


// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray  arrayWithObjects: myPhotos, nil] forKeys:[NSArray arrayWithObjects: self.photoName, nil]];

NSString *error = nil;

// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

// check is plistData exists
if(plistData) {
    // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
}else{
        NSLog(@"Error in saveData: %@", error);
        [error release];
}

The below is what we ended up with

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Gallery.plist"];

// Get current content.
NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// Make a mutable copy.
NSMutableDictionary *newContent = [[oldContent mutableCopy] autorelease];
// Add new stuff.

NSMutableArray *myPhotos = [[NSMutableArray alloc] initWithCapacity:2];
[myPhotos addObject:@"Attribute 1"];
[myPhotos addObject:@"Attribute 2"];

[newContent setObject:myPhotos forKey:self.filePath];

// Now, write the plist:
[newContent writeToFile:plistPath atomically:YES];

2条回答
叛逆
2楼-- · 2019-01-27 01:04

You have a Dictionary to begin with,

124818240124810284.JPG //[valueForKey: 124818240124810284.JPG] 



                    item 0          String     Some Attribute 1 //[valueForKey:124818240124810284.JPG] valueForKey:Item 0];
                    item 1          String     Some Attribute 2 //[valueForKey:124818240124810284.JPG] valueForKey:Item 1];


    354273577823597297.JPG    //[valueForKey: 354273577823597297.JPG]    
                    item 0          String     Some Attribute 1 //[valueForKey: 354273577823597297.JPG] valueForKey:Item 0];
                    item 1          String     Some Attribute 2 //[valueForKey: 354273577823597297.JPG] valueForKey:Item 1;

And so on.....To add to it create a new dictionary with your new value/key pairs :

 NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Attribute 0",@"Item 0",nil];

Make a new dictionary:

   NSMutableDictionary *newEntry = [[NSMutableDictionary alloc]initWithObjectsAndKeys:newAttributes,@"xxxxx.jpg", nil];//Makes a new dictionary

Or to append the data to the existing dictionary:

[dictionaryToAppend setValue:newAttribues ForKey:xxxxx.jpg"];
查看更多
爷、活的狠高调
3楼-- · 2019-01-27 01:06

You do it like this:

// Get current content.
NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// Make a mutable copy.
NSMutableDictionary *newContent = [[oldContent mutableCopy] autorelease];
// Add new stuff.
[newContent setObject:newObject forKey:newKey];
// Now, write the plist:
[newContent writeToFile:plistPath atomically:YES];

There is no need to use NSPropertyListSerialization at all here.

查看更多
登录 后发表回答