Introduction to “archiving”
Persisting data in a property list (plist) is easy, and is good for simple objects. When your needs are more complicated, and when you need to persist “model” objects that you have designed, you cannot use a property list. However, you can use archiving.
On other software development platforms, namely the .NET Framework and Java, “serialization” is the term used for the process to convert an object into a form that can be persisted, and perhaps delivered to another process somewhere. The term “deserialization” is used for the process to convert the persisted form back into an object within a process.
In Cocoa apps, the term “archiving” is used, rather than “serialization”. To you, the meaning is similar.
NSKeyedArchiver, NSKeyedUnarchiver, NSCoding
To implement archiving, we use a method in the NSKeyedArchiver class to save an object (which may be simple, or a very complex object hierarchy) to a data file.
To unarchive – read from a data file into an object – we use a method in the NSKeyedUnarchiver class.
Objects that conform to the NSCoding protocol can be archived. Most Foundation and UIKit classes conform to NSCoding, but NSObject, and a few others, do not.
When you create your own “model” objects, they typically are a subclass of NSObject. Therefore, you must make your “model” object conform to the NSCoding protocol, which in turn requires you to implement two methods. These two methods simply describe how to archive and unarchive instances of your model class.
- To create/write/save an archive, use [NSKeyedArchiver archiveRootObject... toFile...
- To open/read an archive, use [NSKeyedUnarchiver unarchiveObjectWithFile...
- If required, write code to make your model objects conform to NSCoding
Conform to NSCoding
There are two required methods for the NSCoding protocol: encodeWithCoder:, and initWithCoder:
The encodeWithCoder: method
This calls methods that encode the model object's instance variables, which prepares it for saving (archiving). See the example app for an implementation, and the documentation for more information.
The initWithCoder: method
This calls methods that initializes the model object's instance variables from the stored data (unarchiving). As above, see the example app for an implementation, and the documentation.
Archive and unarchive
To "round trip" an archive, the archive/unarchive methods from above are used. Assuming a "urlToFile" file URL, and "myObject" is my model object, or some other kind of object which contains my model object(s), here's how we create/write an archive for "myObject":
[NSKeyedArchiver archiveRootObject:myObject toFile:[urlToFile path]];
In the NSURL Class Reference, the - path method's "return value" section tells us the following:
If this URL object contains a file URL (as determined with isFileURL), the return value of this method is suitable for input into methods of NSFileManager or NSPathUtilities.
Here's how you open/read an archive, and place the results into an already-declared "myObject". Again, assume "urlToFile"...:
myObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[urlToFile path]]
Note that we are using the recommended NSURL object for the file. We send it the - path message to return a string that can be used by NSKeyedArchiver and NSKeyedUnarchiver.