Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

ipad - Appending data to PLIST - iPhone

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];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...