Codable is a type alias that combines:
Encodable + Decodable
It is used to convert between Swift models and JSON data.
JSON → Decodable → Swift Model → Encodable → JSON
Codable provides a two-way data flow between client and server.
In networking:
GET → receive JSON → decode into models
POST → send data → encode from models
Codable handles both operations cleanly.
struct UserResponse: Codable {
let name: String
let email: String
let id: String
let joining: String
}
A Codable model can both:
decode JSON → Swift
encode Swift → JSON