An encoder that stores an object’s data to an archive referenced by keys.
- iOS 2.0+
- macOS 10.2+
- Mac Catalyst 13.0+Beta
- tvOS 9.0+
- watchOS 2.0+
NSKeyed, a concrete subclass of
NSCoder, provides a way to encode objects (and scalar values) into an architecture-independent suitable for storage in a file. When you archive a set of objects, the archiver writes the class information and instance variables for each object to the archive. The companion class
NSKeyed decodes the data in an archive and creates a set of objects equivalent to the original set.
A keyed archive differs from a non-keyed archive in that all the objects and values encoded into the archive have names, or keys. When decoding a non-keyed archive, the decoder must decode values in the same order the original encoder used. When decoding a keyed archive, the decoder requests values by name, meaning it can decode values out of sequence or not at all. Keyed archives, therefore, provide better support for forward and backward compatibility.
The keys given to encoded values must be unique only within the scope of the currently-encoding object. A keyed archive is hierarchical, so the keys used by object A to encode its instance variables don’t conflict with the keys used by object B. This is true even if A and B are instances of the same class. Within a single object, however, the keys used by a subclass can conflict with keys used in its superclasses.