Archiving Objects
Archiving an
object
(or a collection of interconnected objects, known as a graph) into a
NSData
representation is often useful or
necessary. Objects that are archived into an
NSData
object can be transported over network
connections or interprocess communication channels and saved to the
filesystem. Later, the original graph of objects can be reconstituted
from the archive data.
Foundation provides five classes to support the creation and
extraction of archives, all subclasses of NSCoder
:
NSArchiver
NSUnarchiver
NSKeyedArchiver
NSKeyedUnarchiver
NSPortCoder
NSCoder
declares the common interface for
encoding and decoding objects and other Objective-C data types. For
example, the encodeObject
method encodes an object
into an archive, and methods such as encodeInt
:
and encodeRect
: support encoding C data types such
as integers and common Cocoa data structures.
NSCoder
does not implement these methods; it is an
abstract class. Rather, subclasses implement the appropriate methods
for their particular purpose.
NSArchiver
and
NSUnarchiver
provide a straightforward way of encoding
and decoding objects and scalars, but they have limitations. The
biggest limitation is that objects in an archive can be decoded only
in the same order in which they were encoded. Because of this
constraint, changing an encoding system is difficult once it has been
established publicly.
Keyed Archiving
The
NSKeyedArchiver
and
NSKeyedUnarchiver
classes solve this problem by associating ...
Get Cocoa in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.