Archives and Serialization
Convert objects and values to and from property list, JSON, and other flat binary representations.
Overview
Use these APIs to convert your app’s in-memory types to representations suitable for serialization over I/O and network interfaces or to long-term storage.
In Swift, the standard library defines the Encodable, Decodable, and Codable types, along with Encoder and Decoder APIs to perform encoding and decoding, as described in Encoding, Decoding, and Serialization. Foundation extends this with the EncodableWithConfiguration and DecodableWithConfiguration protocols, used for types that require additional static information to encode and decode, such as AttributedString.
In Objective-C, NSCoding defines a protocol for encoding and decoding objects. When adding serialization to your own types, you should also adopt NSSecureCoding. This protocol adds protection against security vulnerabilities introduced by instantiating arbitrary objects as part of the decoding process.
Many system frameworks use these types. When working with external systems, such as URL endpoints, use the JSON and XML APIs to serialize your app’s types to standard formats.
Topics
Adopting Codability
Serializing Arbitrary Payloads
CodableWithConfigurationCodableConfigurationDecodableWithConfigurationDecodingConfigurationProvidingEncodableWithConfigurationEncodingConfigurationProviding
JSON
Property Lists
XML
Keyed Archivers
NSKeyedArchiverNSKeyedArchiverDelegateNSKeyedUnarchiverNSKeyedUnarchiverDelegateNSCoderNSSecureUnarchiveFromDataTransformer