Tsallaka zuwa abun ciki
Justice Tech School

Codable and JSON

Codable converts types to and from JSON with JSONEncoder and JSONDecoder.

Example
import Foundation
struct P: Codable { var name: String; var age: Int }
let data = try JSONEncoder().encode(P(name: "Ada", age: 30))
print(String(data: data, encoding: .utf8)!)
let p = try JSONDecoder().decode(P.self, from: data)
print(p.name)

Exercise

Adopt Codable.

struct P:  {}

Quiz Test

What does Codable combine?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.