Open ClassFoundation5.9.0
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 are NSKeyedArchiver
and NSKeyedUnarchiver
. Concrete subclasses of NSCoder
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.
class NSCoder
NSCoder
operates on objects, scalars, C arrays, structures, and strings, and on pointers to these types. It does not handle types whose implementation varies across platforms, such as UnsafeRawPointer
, closures, and long chains of pointers. A coder object stores object type information along with the data, so an object decoded from a stream of bytes is normally of the same class as the object that was originally encoded into the stream. An object can change its class when encoded, however; this is described in Archives and Serializations Programming Guide.
Superclasses
class NSObject
The root class of most Foundation class hierarchies.
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
var allowedClasses: [AnyClass]?
The array of coded classes allowed for secure coding.
var allowsKeyedCoding: Bool
A Boolean value that indicates whether the receiver supports keyed coding of objects.
var decodingFailurePolicy: NSCoder.DecodingFailurePolicy
var error: Error?
var requiresSecureCoding: Bool
Boolean value that indicates whether the coder requires secure coding.
var systemVersion: UInt32
The system version in effect for the archive.
func containsValue(forKey: String
) -> Bool Returns a Boolean value that indicates whether an encoded value is available for a string.
func decodeArray(ofObjCType: UnsafePointer<Int8>, count: Int, at: UnsafeMutableRawPointer
) func decodeBool(forKey: String
) -> Bool Decodes and returns a Boolean value that was previously encoded with
encode(_:forKey:)
and associated with the stringkey
.func decodeCInt(forKey: String
) -> Int32 Decodes and returns an int value that was previously encoded with
encodeCInt(_:forKey:)
orencode(_:forKey:)
and associated with the stringkey
.func decodeData(
) -> Data? Decodes and returns a
Data
object that was previously encoded withencode(_:)
. Subclasses must override this method.func decodeDouble(forKey: String
) -> Double Decodes and returns a double value that was previously encoded with
encodeCInt(_:forKey:)
orencode(_:forKey:)
and associated with the stringkey
.func decodeFloat(forKey: String
) -> Float Decodes and returns a float value that was previously encoded with
encodeCInt(_:forKey:)
orencode(_:forKey:)
and associated with the stringkey
.func decodeInt32(forKey: String
) -> Int32 Decodes and returns a 32-bit integer value that was previously encoded with
encodeCInt(_:forKey:)
orencode(_:forKey:)
and associated with the stringkey
.func decodeInt64(forKey: String
) -> Int64 Decodes and returns a 64-bit integer value that was previously encoded with
encodeCInt(_:forKey:)
orencode(_:forKey:)
and associated with the stringkey
.func decodeInteger(forKey: String
) -> Int Decodes and returns an integer value that was previously encoded with
encodeCInt(_:forKey:)
orencode(_:forKey:)
and associated with the stringkey
.func decodeObject(
) -> Any? Decodes an Objective-C object that was previously encoded with any of the
encode...Object
methods.func decodeObject(forKey: String
) -> Any? Decodes and returns an Objective-C object that was previously encoded with
encode(_:forKey:)
orencodeConditionalObject(_:forKey:)
and associated with the stringkey
.func decodeObject<DecodedObjectType>(of: DecodedObjectType.Type, forKey: String
) -> DecodedObjectType? func decodeObject(of: [AnyClass]?, forKey: String
) -> Any? Decodes an object for the key, restricted to the specified
classes
.func decodePoint(
) -> NSPoint func decodePoint(forKey: String
) -> NSPoint func decodePropertyList(
) -> Any? Decodes a property list that was previously encoded with
encodePropertyList(_:)
.func decodePropertyList(forKey: String
) -> Any? Returns a decoded property list for the specified key.
func decodeRect(
) -> NSRect func decodeRect(forKey: String
) -> NSRect func decodeSize(
) -> NSSize func decodeSize(forKey: String
) -> NSSize func decodeValue(ofObjCType: UnsafePointer<Int8>, at: UnsafeMutableRawPointer
) Decodes a single value, whose Objective-C type is given by
type
.func encode(Data
) Encodes a given
Data
object.func encode(NSRect
) func encode(NSSize
) func encode(NSPoint
) func encode(Any
?) Encodes
object
.func encode(NSRect, forKey: String
) func encode(NSSize, forKey: String
) func encode(NSPoint, forKey: String
) func encode(Bool, forKey: String
) Encodes
boolv
and associates it with the stringkey
.func encode(Double, forKey: String
) Encodes
realv
and associates it with the stringkey
.func encode(Float, forKey: String
) Encodes
realv
and associates it with the stringkey
.func encode(Int, forKey: String
) Encodes a given integer number and associates it with a given key.
func encode(Int32, forKey: String
) Encodes the 32-bit integer
intv
and associates it with the stringkey
.func encode(Int64, forKey: String
) Encodes the 64-bit integer
intv
and associates it with the stringkey
.func encode(Any
?, forKey: String) Encodes the object
objv
and associates it with the stringkey
.func encodeArray(ofObjCType: UnsafePointer<Int8>, count: Int, at: UnsafeRawPointer
) Encodes an array of
count
items, whose Objective-C type is given bytype
.func encodeBycopyObject(Any
?) Can be overridden by subclasses to encode
anObject
so that a copy, rather than a proxy, is created upon decoding.func encodeByrefObject(Any
?) Can be overridden by subclasses to encode
anObject
so that a proxy, rather than a copy, is created upon decoding.func encodeBytes(UnsafeRawPointer
?, length: Int) Encodes a buffer of data whose types are unspecified.
func encodeBytes(UnsafePointer
<UInt8>?, length: Int, forKey: String) Encodes a buffer of data,
bytesp
, whose length is specified bylenv
, and associates it with the stringkey
.func encodeConditionalObject(Any
?) Can be overridden by subclasses to conditionally encode
object
, preserving common references to that object.func encodeConditionalObject(Any
?, forKey: String) Conditionally encodes a reference to
objv
and associates it with the stringkey
only ifobjv
has been unconditionally encoded withencode(_:forKey:)
.func encodePropertyList(Any
) Encodes the property list
aPropertyList
.func encodeRootObject(Any
) Can be overridden by subclasses to encode an interconnected group of Objective-C objects, starting with
rootObject
.func encodeValue(ofObjCType: UnsafePointer<Int8>, at: UnsafeRawPointer
) Must be overridden by subclasses to encode a single value residing at
addr
, whose Objective-C type is given bytype
.func failWithError(Error
) func version(forClassName: String
) -> Int This method is present for historical reasons and is not used with keyed archivers.
func withDecodedUnsafeBytes<ResultType>(forKey: String, body: (UnsafeRawBufferPointer?) throws -> ResultType
) rethrows -> ResultType enum DecodingFailurePolicy
Describes the action an
NSCoder
should take when it encounters decode failures (e.g. corrupt data) for non-TopLevel decodes. Darwin platfrom supports exceptions here, and there may be other approaches supported in the future, so its included for completeness.func withDecodedUnsafeBufferPointer<ResultType>(forKey: String, body: (UnsafeBufferPointer<UInt8>?) throws -> ResultType
) rethrows -> ResultType
Subclasses
class NSKeyedArchiver
NSKeyedArchiver
, a concrete subclass ofNSCoder
, 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 NSKeyedUnarchiver