Other Structures
-
A type-erased
Codablevalue.The
AnyCodabletype forwards encoding and decoding responsibilities to an underlying value, hiding its specific underlying type.You can encode or decode mixed-type values in dictionaries and other collections that require
EncodableorDecodableconformance by declaring their contained type to beAnyCodable.See also
AnyEncodableSee also
AnyDecodableDeclaration
Swift
public struct AnyCodable : Codable
-
A type-erased
Decodablevalue.The
AnyDecodabletype forwards decoding responsibilities to an underlying value, hiding its specific underlying type.You can decode mixed-type values in dictionaries and other collections that require
Decodableconformance by declaring their contained type to beAnyDecodable:let json = """ { "boolean": true, "integer": 1, "double": 3.14159265358979323846, "string": "string", "array": [1, 2, 3], "nested": { "a": "alpha", "b": "bravo", "c": "charlie" } } """.data(using: .utf8)! let decoder = JSONDecoder() let dictionary = try! decoder.decode([String: AnyCodable].self, from: json)Declaration
Swift
public struct AnyDecodable : Decodable
-
A type-erased
Encodablevalue.The
AnyEncodabletype forwards encoding responsibilities to an underlying value, hiding its specific underlying type.You can encode mixed-type values in dictionaries and other collections that require
Encodableconformance by declaring their contained type to beAnyEncodable:let dictionary: [String: AnyEncodable] = [ "boolean": true, "integer": 1, "double": 3.14159265358979323846, "string": "string", "array": [1, 2, 3], "nested": [ "a": "alpha", "b": "bravo", "c": "charlie" ] ] let encoder = JSONEncoder() let json = try! encoder.encode(dictionary)Declaration
Swift
public struct AnyEncodable : Encodable
Other Structures Reference