views:

184

answers:

2

Hi.

I am trying to write some data from an NSMutableArray to a plist, while keep the old plists content.

The function writeToFile:atomically: overwrites the old contents with the new, I want to append the objects in the new array to the plist.

How can this be done? Thank you.

How can you check for duplicates while doing this?

+4  A: 

You could load the contents of the file into an array, then use addObjectsFromArray: in your MutableArray to load the contents into that, then use the writeToFile feature.

i.e.

NSArray *oldArray = [NSArray arrayWithContentsOfFile:filePath];
[newMutableArray addObjectsFromArray:oldArray];

.....

[newMutableArray writeToFile:filePath atomically:YES];
djhworld
I think I'm going to go with this, as it seems to be simpler. Thanks :)
Emil
+1 This is the only way to do this and maintain the structure of the array in the file.
TechZen
A: 

Just create use an instance of NSFileHandle and use -seekToEndOfFile, then do your writing from the array.

rscott
-1 This will append data to the end of the file but it will be unusable gibberish and not a serialized NSMutableArray.
TechZen
Yeah, that's what I first thought about.
Emil