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, NSKeyedArchiver, NSKeyedUnarchiver, and NSPortCoder. 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 allowsKeyedCoding: Bool

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

func containsValue(forKey: String)

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

Encoding General Data

func encodeArray(ofObjCType: UnsafePointer<Int8>, count: Int, at: UnsafeRawPointer)

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

func encode(Bool, forKey: String)

Encodes boolv and associates it with the string key.

func encodeBycopyObject(Any?)

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

func encodeByrefObject(Any?)

Can be overridden by subclasses to encode object 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 by lenv, and associates it with the string key.

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 string key only if objv has been unconditionally encoded with encode(_:forKey:).

func encode(Data)

Encodes a given NSData object.

func encode(Double, forKey: String)

Encodes realv and associates it with the string key.

func encode(Float, forKey: String)

Encodes realv and associates it with the string key.

func encodeCInt(Int32, forKey: String)

Encodes intv and associates it with the string key.

func encode(Int, forKey: String)

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

func encode(Int32, forKey: String)

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

func encode(Int64, forKey: String)

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

func encode(Any?)

Encodes object.

func encode(Any?, forKey: String)

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

func encode(NSPoint)

Encodes point.

func encode(NSPoint, forKey: String)

Encodes point and associates it with the string key.

func encodePropertyList(Any)

Encodes the property list aPropertyList.

func encode(NSRect)

Encodes rect.

func encode(NSRect, forKey: String)

Encodes rect and associates it with the string key.

func encodeRootObject(Any)

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

func encode(NSSize)

Encodes size.

func encode(NSSize, forKey: String)

Encodes size and associates it with the string key.

func encodeValue(ofObjCType: UnsafePointer<Int8>, at: UnsafeRawPointer)

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

Encoding Geometry-Based Data

func encode(CGPoint, forKey: String)

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

func encode(CGRect, forKey: String)

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

func encode(CGSize, forKey: String)

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

func encode(CGAffineTransform, forKey: String)

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

func encode(UIEdgeInsets, forKey: String)

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

func encode(UIOffset, forKey: String)

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

func encode(CGVector, forKey: String)

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

Encoding Core Media Time Structures

func encode(CMTime, forKey: String)

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

func encode(CMTimeRange, forKey: String)

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

func encode(CMTimeMapping, forKey: String)

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

Decoding General Data

func decodeArray(ofObjCType: UnsafePointer<Int8>, count: Int, at: UnsafeMutableRawPointer)

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

func decodeBool(forKey: String)

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

func decodeBytes(forKey: String, returnedLength: UnsafeMutablePointer<Int>?)

Decodes a buffer of data that was previously encoded with encodeBytes(_:length:forKey:) and associated with the string key.

func decodeBytes(withReturnedLength: UnsafeMutablePointer<Int>)

Decodes a buffer of data whose types are unspecified.

func decodeData()

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

func decodeDouble(forKey: String)

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

func decodeFloat(forKey: String)

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

func decodeCInt(forKey: String)

Decodes and returns an int value that was previously encoded with encodeCInt(_:forKey:), encode(_:forKey:), encode(_:forKey:), or encode(_:forKey:) and associated with the string key.

func decodeInteger(forKey: String)

Decodes and returns an NSInteger value that was previously encoded with encodeCInt(_:forKey:), encode(_:forKey:), encode(_:forKey:), or encode(_:forKey:) and associated with the string key.

func decodeInt32(forKey: String)

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

func decodeInt64(forKey: String)

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

func decodeObject()

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

func decodeObject(forKey: String)

Decodes and returns an Objective-C object that was previously encoded with encode(_:forKey:) or encodeConditionalObject(_:forKey:) and associated with the string key.

func decodePoint()

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

func decodePoint(forKey: String)

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

func decodePropertyList()

Decodes a property list that was previously encoded with encodePropertyList(_:).

func decodeRect()

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

func decodeRect(forKey: String)

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

func decodeSize()

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

func decodeSize(forKey: String)

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

func decodeValue(ofObjCType: UnsafePointer<Int8>, at: UnsafeMutableRawPointer)

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

func decodePropertyList(forKey: String)

Returns a decoded property list for the specified key.

Decoding Geometry-Based Data

func decodeCGPoint(forKey: String)

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

func decodeCGRect(forKey: String)

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

func decodeCGSize(forKey: String)

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

func decodeCGAffineTransform(forKey: String)

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

func decodeUIEdgeInsets(forKey: String)

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

func decodeUIOffset(forKey: String)

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

func decodeCGVector(forKey: String)

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

Decoding Core Media Time Structures

func decodeTime(forKey: String)

Returns the CMTime structure associated with a given key.

func decodeTimeRange(forKey: String)

Returns the CMTimeRange structure associated with a given key.

func decodeTimeMapping(forKey: String)

Returns the CMTimeMapping structure associated with a given key.

Secure Coding

var requiresSecureCoding: Bool

Boolean value that indicates whether the coder requires secure coding.

var allowedClasses: Set<AnyHashable>?

The set of coded classes allowed for secure coding.

Getting Version Information

var systemVersion: UInt32

The system version in effect for the archive.

func version(forClassName: String)

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

Relationships

Inherits From

Conforms To