AnyEncodable
public struct AnyEncodable : Encodable
extension AnyEncodable: Equatable
extension AnyEncodable: CustomStringConvertible
extension AnyEncodable: CustomDebugStringConvertible
extension AnyEncodable: ExpressibleByNilLiteral,
ExpressibleByBooleanLiteral,
ExpressibleByIntegerLiteral,
ExpressibleByFloatLiteral,
ExpressibleByStringLiteral,
ExpressibleByArrayLiteral,
ExpressibleByDictionaryLiteral
A type-erased Encodable
value.
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": 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 let value: Any
-
Declaration
Swift
public init<T>(_ value: T?)
-
Declaration
Swift
public static func == (lhs: AnyEncodable, rhs: AnyEncodable) -> Bool
-
Declaration
Swift
public var description: String { get }
-
Declaration
Swift
public var debugDescription: String { get }
-
Declaration
Swift
public init(nilLiteral: ())
-
Declaration
Swift
public init(booleanLiteral value: Bool)
-
Declaration
Swift
public init(integerLiteral value: Int)
-
Declaration
Swift
public init(floatLiteral value: Double)
-
Declaration
Swift
public init(extendedGraphemeClusterLiteral value: String)
-
Declaration
Swift
public init(stringLiteral value: String)
-
Declaration
Swift
public init(arrayLiteral elements: Any...)
-
Declaration
Swift
public init(dictionaryLiteral elements: (AnyHashable, Any)...)