Decoder
The Decoder
protocol in Swift translates data from external formats such as JSON or XML into native Swift types. All data intended for decoding should conform to the Decodable
protocol. While creating custom decoders is possible, existing classes like JSONDecoder
and PropertyListDecoder
cover a wide range of use cases, making the need for custom decoders relatively rare.
Syntax
struct SomeStruct: Decodable {
// Define properties that match the data structure
init(from decoder: Decoder) throws {
// Custom decoding logic, if needed
}
}
let decoder = JSONDecoder()
let someData: Data
let someInstance = try decoder.decode(SomeStruct.self, from: someData)
SomeStruct
conforms to theDecodable
protocol indicating it can decode an external data source.- The
init(from:)
initializer is optional and provides a way to implement custom decoding logic. - Instantiate a decoder class, such as
JSONDecoder
, to convert the encoded data into the desired Swift type. someData
is aData
object containing the data to decode.- The
decode(_:from:)
method transforms the data into an instance ofSomeStruct
.
Example
In the following example, a JSON object is decoded into a Book
struct.
import Foundationstruct Book: Decodable {let title: Stringlet author: Stringlet publicationYear: Int}// JSON data for a booklet bookJson = """{"title": "Journey to the Center of the Earth","author": "Jules Verne","publicationYear": 1864}"""let bookData = Data(bookJson.utf8)let jsonDecoder = JSONDecoder()let book = try jsonDecoder.decode(Book.self, from: bookData)print("\(book.title) by \(book.author)")
In this example, a Book
struct has three properties, title
, author
, and publicationYear
, that match the keys in the JSON data. The init(from:)
initializer is not implemented because the property labels match the JSON data keys. The JSON data is then decoded into a Book
instance using the decode(_:from:)
method of the JSONDecoder
class.
This will output the following:
Journey to the Center of the Earth by Jules Verne
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Swift on Codecademy
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours