Appending data to PLIST - iPhone

耗尽温柔 提交于 2019-11-28 05:53:41

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/8884215/appending-data-to-plist-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!