Generic Instance Method

encode(_:)

Returns a JSON-encoded representation of the value you supply.

Declaration

func encode<T>(_ value: T) throws -> Data where T : Encodable

Parameters

value

The value to encode as JSON.

Discussion

If there's a problem encoding the value you supply, 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 JSON array or JSON object—this method throws the EncodingError.invalidValue(_:_:) error.

  • The value contains an exceptional floating-point number (such as infinity or nan) and you're using the default JSONEncoder.NonConformingFloatEncodingStrategy—this method throws the EncodingError.invalidValue(_:_:) error.

Relationships

From Protocol

See Also

First Steps

init()

Creates a new, reusable JSON encoder with the default formatting settings and encoding strategies.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software