Skip to content

Instance Method

decode(_:from:)

iOS 8.0+ iPadOS 8.0+ Mac Catalyst 8.0+ macOS 10.10+ tvOS 9.0+ watchOS 2.0+
Returns a value of the type you specify, decoded from a JSON object.
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable

Parameters

type

The type of the value to decode.

data

The data to decode from.

Return Value

A value of the requested type.

Discussion

Throws

DecodingError.dataCorrupted if values requested from the payload are corrupted, or if the given data is not valid JSON.

Throws

An error if any value throws an error during decoding.