AnyEncoder
public protocol AnyEncoder
An encoder type.
Swift has no common built-in protocol for all encoder types. This protocol remedies that shortcoming.
-
Returns an encoded representation, a
Data
object using an encoder conforming to theAnyEncoder
protocol, of a type-inferred value.If there’s a problem encoding the type-inferred value, this method throws an error based on the type of problem:
- The value fails to encode or contains a nested value that fails to encode—this method throws the corresponding error.
The value can’t be encoded as a array or object (in the format specified by the encoder)—this method throws the
EncodingError.invalidValue(_:_:)
error.
Declaration
Swift
func encode<T>(_ value: T) throws -> Data where T : Encodable
Parameters
type
The type, adopting to the
Decodable
protocol, of the value to decode from the suppliedData
object.value
The value, adopting the
Encodable
protocol, to encode as aData
object using an encoder conforming to theAnyEncoder
protocol.Return Value
A
Data
object, encoded using an encoder conforming to theAnyEncoder
protocol.