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 are NSArchiver, NSUnarchiver, NSKeyed​Archiver, NSKeyed​Unarchiver, and NSPort​Coder. 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.

Overview

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 union, void *, function pointers, 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.

The AV Foundation framework adds methods to the NSCoder class to make it easier to create archives including Core Media time structures, and extract Core Media time structure from archives.

Subclassing Notes

For details of how to create a subclass of NSCoder, see Subclassing NSCoder in Archives and Serializations Programming Guide.

Symbols

Inspecting a Coder

var allows​Keyed​Coding:​ Bool

A Boolean value that indicates whether the receiver supports keyed coding of objects.

func contains​Value(for​Key:​ String)

Returns a Boolean value that indicates whether an encoded value is available for a string.

Encoding General Data

func encode​Array(of​Obj​CType:​ Unsafe​Pointer<Int8>, count:​ Int, at:​ Unsafe​Raw​Pointer)

Encodes an array of count items, whose Objective-C type is given by item​Type.

func encode(Bool, for​Key:​ String)

Encodes boolv and associates it with the string key.

func encode​Bycopy​Object(Any?)

Can be overridden by subclasses to encode object so that a copy, rather than a proxy, is created upon decoding.

func encode​Byref​Object(Any?)

Can be overridden by subclasses to encode object so that a proxy, rather than a copy, is created upon decoding.

func encode​Bytes(Unsafe​Raw​Pointer?, length:​ Int)

Encodes a buffer of data whose types are unspecified.

func encode​Bytes(Unsafe​Pointer<UInt8>?, length:​ Int, for​Key:​ String)

Encodes a buffer of data, bytesp, whose length is specified by lenv, and associates it with the string key.

func encode​Conditional​Object(Any?)

Can be overridden by subclasses to conditionally encode object, preserving common references to that object.

func encode​Conditional​Object(Any?, for​Key:​ String)

Conditionally encodes a reference to objv and associates it with the string key only if objv has been unconditionally encoded with encode(_:​for​Key:​).

func encode(Data)

Encodes a given NSData object.

func encode(Double, for​Key:​ String)

Encodes realv and associates it with the string key.

func encode(Float, for​Key:​ String)

Encodes realv and associates it with the string key.

func encode​CInt(Int32, for​Key:​ String)

Encodes intv and associates it with the string key.

func encode(Int, for​Key:​ String)

Encodes a given NSInteger and associates it with a given key.

func encode(Int32, for​Key:​ String)

Encodes the 32-bit integer intv and associates it with the string key.

func encode(Int64, for​Key:​ String)

Encodes the 64-bit integer intv and associates it with the string key.

func encode(Any?)

Encodes object.

func encode(Any?, for​Key:​ String)

Encodes the object objv and associates it with the string key.

func encode(NSPoint)

Encodes point.

func encode(NSPoint, for​Key:​ String)

Encodes point and associates it with the string key.

func encode​Property​List(Any)

Encodes the property list a​Property​List.

func encode(NSRect)

Encodes rect.

func encode(NSRect, for​Key:​ String)

Encodes rect and associates it with the string key.

func encode​Root​Object(Any)

Can be overridden by subclasses to encode an interconnected group of Objective-C objects, starting with root​Object.

func encode(NSSize)

Encodes size.

func encode(NSSize, for​Key:​ String)

Encodes size and associates it with the string key.

func encode​Value(of​Obj​CType:​ Unsafe​Pointer<Int8>, at:​ Unsafe​Raw​Pointer)

Must be overridden by subclasses to encode a single value residing at address, whose Objective-C type is given by value​Type.

Encoding Geometry-Based Data

func encode(CGPoint, for​Key:​ String)

Encodes a point and associates it with the specified key in the receiver’s archive.

func encode(CGRect, for​Key:​ String)

Encodes a rectangle and associates it with the specified key in the receiver’s archive.

func encode(CGSize, for​Key:​ String)

Encodes size information and associates it with the specified key in the receiver’s archive.

func encode(CGAffine​Transform, for​Key:​ String)

Encodes an affine transform and associates it with the specified key in the receiver’s archive.

func encode(UIEdge​Insets, for​Key:​ String)

Encodes edge inset data and associates it with the specified key in the receiver’s archive.

func encode(UIOffset, for​Key:​ String)

Encodes offset data and associates it with the specified key in the receiver’s archive.

func encode(CGVector, for​Key:​ String)

Encodes vector data and associates it with the specified key in the coder’s archive.

Encoding Core Media Time Structures

func encode(CMTime, for​Key:​ String)

Encodes a given CMTime structure and associates it with a specified key.

func encode(CMTime​Range, for​Key:​ String)

Encodes a given CMTime​Range structure and associates it with a specified key.

func encode(CMTime​Mapping, for​Key:​ String)

Encodes a given CMTime​Mapping structure and associates it with a specified key.

Decoding General Data

func decode​Array(of​Obj​CType:​ Unsafe​Pointer<Int8>, count:​ Int, at:​ Unsafe​Mutable​Raw​Pointer)

Decodes an array of count items, whose Objective-C type is given by item​Type.

func decode​Bool(for​Key:​ String)

Decodes and returns a boolean value that was previously encoded with encode(_:​for​Key:​) and associated with the string key.

func decode​Bytes(for​Key:​ String, returned​Length:​ Unsafe​Mutable​Pointer<Int>?)

Decodes a buffer of data that was previously encoded with encode​Bytes(_:​length:​for​Key:​) and associated with the string key.

func decode​Data()

Decodes and returns an NSData object that was previously encoded with encode(_:​). Subclasses must override this method.

func decode​Double(for​Key:​ String)

Decodes and returns a double value that was previously encoded with either encode(_:​for​Key:​) or encode(_:​for​Key:​) and associated with the string key.

func decode​Float(for​Key:​ String)

Decodes and returns a float value that was previously encoded with encode(_:​for​Key:​) or encode(_:​for​Key:​) and associated with the string key.

func decode​CInt(for​Key:​ String)

Decodes and returns an int value that was previously encoded with encode​CInt(_:​for​Key:​), encode(_:​for​Key:​), encode(_:​for​Key:​), or encode(_:​for​Key:​) and associated with the string key.

func decode​Integer(for​Key:​ String)

Decodes and returns an NSInteger value that was previously encoded with encode​CInt(_:​for​Key:​), encode(_:​for​Key:​), encode(_:​for​Key:​), or encode(_:​for​Key:​) and associated with the string key.

func decode​Int32(for​Key:​ String)

Decodes and returns a 32-bit integer value that was previously encoded with encode​CInt(_:​for​Key:​), encode(_:​for​Key:​), encode(_:​for​Key:​), or encode(_:​for​Key:​) and associated with the string key.

func decode​Int64(for​Key:​ String)

Decodes and returns a 64-bit integer value that was previously encoded with encode​CInt(_:​for​Key:​), encode(_:​for​Key:​), encode(_:​for​Key:​), or encode(_:​for​Key:​) and associated with the string key.

func decode​Object()

Decodes an Objective-C object that was previously encoded with any of the encode...Object:​ methods.

func decode​Object(for​Key:​ String)

Decodes and returns an Objective-C object that was previously encoded with encode(_:​for​Key:​) or encode​Conditional​Object(_:​for​Key:​) and associated with the string key.

func decode​Point()

Decodes and returns an NSPoint structure that was previously encoded with encode(_:​).

func decode​Point(for​Key:​ String)

Decodes and returns an NSPoint structure that was previously encoded with encode(_:​for​Key:​).

func decode​Property​List()

Decodes a property list that was previously encoded with encode​Property​List(_:​).

func decode​Rect()

Decodes and returns an NSRect structure that was previously encoded with encode(_:​).

func decode​Rect(for​Key:​ String)

Decodes and returns an NSRect structure that was previously encoded with encode(_:​for​Key:​).

func decode​Size()

Decodes and returns an NSSize structure that was previously encoded with encode(_:​).

func decode​Size(for​Key:​ String)

Decodes and returns an NSSize structure that was previously encoded with encode(_:​for​Key:​).

func decode​Value(of​Obj​CType:​ Unsafe​Pointer<Int8>, at:​ Unsafe​Mutable​Raw​Pointer)

Decodes a single value, whose Objective-C type is given by value​Type.

func decode​Property​List(for​Key:​ String)

Returns a decoded property list for the specified key.

Decoding Geometry-Based Data

func decode​CGPoint(for​Key:​ String)

Decodes and returns the CGPoint structure associated with the specified key in the receiver’s archive.

func decode​CGRect(for​Key:​ String)

Decodes and returns the CGRect structure associated with the specified key in the receiver’s archive.

func decode​CGSize(for​Key:​ String)

Decodes and returns the CGSize structure associated with the specified key in the receiver’s archive.

func decode​CGAffine​Transform(for​Key:​ String)

Decodes and returns the CGAffine​Transform structure associated with the specified key in the receiver’s archive.

func decode​UIEdge​Insets(for​Key:​ String)

Decodes and returns the UIEdge​Insets structure associated with the specified key in the receiver’s archive.

func decode​UIOffset(for​Key:​ String)

Decodes and returns the UIOffset structure associated with the specified key in the receiver’s archive.

func decode​CGVector(for​Key:​ String)

Decodes and returns the CGVector data associated with the specified key in the coder’s archive.

Decoding Core Media Time Structures

func decode​Time(for​Key:​ String)

Returns the CMTime structure associated with a given key.

func decode​Time​Range(for​Key:​ String)

Returns the CMTime​Range structure associated with a given key.

func decode​Time​Mapping(for​Key:​ String)

Returns the CMTime​Mapping structure associated with a given key.

Secure Coding

var requires​Secure​Coding:​ Bool

Boolean value that indicates whether the coder requires secure coding.

var allowed​Classes:​ Set<Any​Hashable>?

The set of coded classes allowed for secure coding.

Getting Version Information

var system​Version:​ UInt32

The system version in effect for the archive.

func version(for​Class​Name:​ String)

This method is present for historical reasons and is not used with keyed archivers.

Relationships

Inherits From