Open ClassFoundation5.9.0
NSKeyedArchiver
NSKeyedArchiver
, a concrete subclass of NSCoder
, provides a way to encode objects (and scalar values) into an architecture-independent format that can be stored in a file. When you archive a set of objects, the class information and instance variables for each object are written to the archive. NSKeyedArchiver
’s companion class, NSKeyedUnarchiver
, decodes the data in an archive and creates a set of objects equivalent to the original set.
class NSKeyedArchiver
A keyed archive differs from a non-keyed archive in that all the objects and values encoded into the archive are given names, or keys. When decoding a non-keyed archive, values have to be decoded in the same order in which they were encoded. When decoding a keyed archive, because values are requested by name, values can be decoded 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 current object being encoded. A keyed archive is hierarchical, so the keys used by object A to encode its instance variables do not conflict with the keys used by object B, 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.
An NSKeyedArchiver
object can write the archive data to a file or to a mutable-data object (an instance of NSMutableData
) that you provide.
Superclasses
class NSCoder
The
NSCoder
abstract class declares the interface used by concrete subclasses to transfer objects and other values between memory and some other format. This capability provides the basis for archiving (where objects and data items are stored on disk) and distribution (where objects and data items are copied between different processes or threads). The concrete subclasses provided by Foundation for these purposes areNSKeyedArchiver
andNSKeyedUnarchiver
. Concrete subclasses ofNSCoder
are referred to in general as coder classes, and instances of these classes as coder objects (or simply coders). A coder object that can only encode values is referred to as an encoder object, and one that can only decode values as a decoder object.
Citizens in Foundation
Conformances
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol NSObjectProtocol
The
NSObjectProtocol
groups methods that are fundamental to all Foundation objects.
Members
convenience init(requiringSecureCoding: Bool
) class func archivedData(withRootObject: Any, requiringSecureCoding: Bool
) throws -> Data class func classNameForClass(AnyClass
) -> String? Returns the class name with which
NSKeyedArchiver
encodes instances of a given class.class func setClassName(String
?, for: AnyClass) Adds a class translation mapping to
NSKeyedArchiver
whereby instances of a given class are encoded with a given class name instead of their real class names.var allowsKeyedCoding: Bool
var delegate: NSKeyedArchiverDelegate?
The archiver’s delegate.
var encodedData: Data
Returns the encoded data for the archiver.
var error: Error?
var outputFormat: PropertyListSerialization.PropertyListFormat
The format in which the receiver encodes its data.
var requiresSecureCoding: Bool
Indicates whether the archiver requires all archived classes to conform to
NSSecureCoding
.var systemVersion: UInt32
func classNameForClass(AnyClass
) -> String? Returns the class name with which the archiver encodes instances of a given class.
func encode(Data
) func encode(Any
?) func encode(Bool, forKey: String
) Encodes a given Boolean value and associates it with a given key.
func encode(Double, forKey: String
) Encodes a given double value and associates it with a given key.
func encode(Float, forKey: String
) Encodes a given float value and associates it with a given key.
func encode(Int, forKey: String
) Encodes a given integer value and associates it with a given key.
func encode(Int32, forKey: String
) Encodes a given 32-bit integer value and associates it with a given key.
func encode(Int64, forKey: String
) Encodes a given 64-bit integer value and associates it with a given key.
func encode(Any
?, forKey: String) Encodes a given object and associates it with a given key.
func encodeBytes(UnsafePointer
<UInt8>?, length: Int, forKey: String) Encodes a given number of bytes from a given C array of bytes and associates them with the a given key.
func encodeConditionalObject(Any
?) func encodeConditionalObject(Any
?, forKey: String) Encodes a reference to a given object and associates it with a given key only if it has been unconditionally encoded elsewhere in the archive with
encode(_:forKey:)
.func encodePropertyList(Any
) func encodePropertyList(Any, forKey: String
) func encodeValue(ofObjCType: UnsafePointer<Int8>, at: UnsafeRawPointer
) func finishEncoding(
) Instructs the archiver to construct the final data stream.
func setClassName(String
?, for: AnyClass) Adds a class translation mapping to
NSKeyedArchiver
whereby instances of a given class are encoded with a given class name instead of their real class names.convenience init(
) convenience init(forWritingWith: NSMutableData
) Returns the archiver, initialized for encoding an archive into a given a mutable-data object.
class func archiveRootObject(Any, toFile: String
) -> Bool Archives an object graph rooted at a given object by encoding it into a data object then atomically writes the resulting data object to a file at a given path, and returns a Boolean value that indicates whether the operation was successful.
class func archivedData(withRootObject: Any
) -> Data Returns an
NSData
object containing the encoded form of the object graph whose root object is given.