Decodable
Anonymous contributor
Anonymous contributor1 total contribution
Anonymous contributor
Published Oct 3, 2023
Contribute to Docs
The Decodable
protocol is used to easily decode JSON or similar data formats like XML. The required initializer is init(from: Decoder)
which creates a new instance by decoding from the given decoder. The Decodable
protocol is often used with the JSONDecoder
class. Decodable
should be used when there’s only a need to read data, when reading and writing are required the Codable
protocol should be implemented.
Syntax
Create a structure to represent the data to decode, followed by Decodable
using the standard syntax of a Swift protocol.
struct DataName: Decodable {
// Properties that conform to Decodable
}
Example
In the example below, a User
struct is defined and the Decodable
protocol is used with JSONDecoder
to convert stored JSON into a data object.
import Foundationstruct User: Decodable {var name: Stringvar age: Intvar location: String}let userJson = """{"name": "Charlotte Lucas","age": 27,"location": "England"}"""let userData = Data(userJson.utf8)let decoder = JSONDecoder()let user = try decoder.decode(User.self, from: userData)print(user.name)
This will result in the following output:
Charlotte Lucas
All contributors
- Anonymous contributorAnonymous contributor1 total contribution
- Anonymous contributor
Looking to contribute?
- 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.