Concept

Codable is a type alias that combines:

Encodable + Decodable

It is used to convert between Swift models and JSON data.


Key Idea

JSON → Decodable → Swift Model → Encodable → JSON

Codable provides a two-way data flow between client and server.


Why Codable is Important

In networking:

GET → receive JSON → decode into models
POST → send data → encode from models

Codable handles both operations cleanly.


Defining a Codable Model

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

Decoding (JSON → Model)