Skip to content

AnyEncodable

bigearsenal edited this page May 17, 2022 · 1 revision

AnyEncodable

A type-erased Encodable value.

@frozen public struct AnyEncodable: Encodable 

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

let dictionary: [String: AnyEncodable] = [
    "boolean": true,
    "integer": 42,
    "double": 3.141592653589793,
    "string": "string",
    "array": [1, 2, 3],
    "nested": [
        "a": "alpha",
        "b": "bravo",
        "c": "charlie"
    ],
    "null": nil
]

let encoder = JSONEncoder()
let json = try! encoder.encode(dictionary)

Inheritance

CustomDebugStringConvertible, CustomStringConvertible, Encodable, Equatable, Hashable

Initializers

init(_:)

public init<T>(_ value: T?) 

Properties

value

public let value: Any

description

public var description: String 

debugDescription

public var debugDescription: String 

Methods

hash(into:)

public func hash(into hasher: inout Hasher) 

Operators

==

public static func == (lhs: AnyEncodable, rhs: AnyEncodable) -> Bool 
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Operators
Extensions
Clone this wiki locally