AnyDecodable

public struct AnyDecodable: Decodable

A type-erased Decodable value.

The AnyDecodable type 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 Decodable conformance by declaring their contained type to be AnyDecodable:

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)
  • The type-erased underlying value.

    Declaration

    Swift

    public let value: Any
  • Declaration

    Swift

    public static func ==(lhs: AnyDecodable, rhs: AnyDecodable) -> Bool
  • A string representation of the decodable object.

    Declaration

    Swift

    public var description: String
  • Returns a string that describes the contents of the receiver for presentation in the debugger.

    Declaration

    Swift

    public var debugDescription: String