Previously, I have an array in which I use NSKeyedArchiver to archieve.
[NSKeyedArchiver archiveRootObject:array toFile:docPath];
Called on applicationWillTerminate and applicationDidEnterBackground.
Upon starting up, in didFinishLaunchingWithOPtions, the array is unarchieved:
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
Everything was good. Since then, I added a Singleton class for Settings variables. I would like to archive the Singleton as well. How would I do that?
The part that confuses me is if I call the same message:
[NSKeyedArchiver archiveRootObject:singleton toFile:docPath];
How can the app know which object I want to unarchive when I call:
[NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
Is it the array or the singleton?
The archiveRootObject accepts an (id) which to me means whatever I want. I can create a data class which includes the array and the singleton. Is that how I am suppose to do this? Is there a better way? Any suggestions are welcomed.
Thanks!
If you want to encode more than one object at the root level, you’ll need to create the archiver using
+allocand-initForWritingWithMutableData:yourself, send it -encodeObject:forKey: messages for each of the objects you want to add to the archive, and finally-finishEncoding. You can then write the data to a file yourself.It’ll look something like this (warning: untested code ahead):
To retrieve the objects, you’ll do essentially the opposite: read a file into an NSData object; create an NSKeyedUnarchiver with
+allocand-initForReadingWithData:; retrieve the objects you care about using-decodeObjectForKey:; and finally call-finishDecoding. You can almost read the sample above from bottom to top with a few name changes.