0Pricing
Swift Academy · Lesson

Codable: Encoding and Decoding JSON

Conforming to Codable, custom CodingKeys and nested container decoding.

Codable Protocol

Codable is a type alias for Encodable & Decodable. Conforming types can be encoded to and decoded from JSON automatically.

struct User: Codable {
  var name: String
  var age: Int
}

Automatic Synthesis

Swift synthesizes Codable implementations when all stored properties are themselves Codable.

struct Product: Codable {
  var id: Int
  var title: String
  var price: Double
  // init(from:) and encode(to:) are auto-generated
}

All lessons in this course

  1. URLSession data(from:) with async/await
  2. Codable: Encoding and Decoding JSON
  3. Error Handling and HTTP Status Codes
  4. Retry Logic and Background URLSession
← Back to Swift Academy