Codable
is a protocol a type can conform to for easy encoding and decoding (serialization and deserialization) of that type. If a type declares that it conforms to Codable
, and all it's properties implement Codable
(Foundation types typically do), the Swift compiler will automatically generate a protocol implementation.
struct Person: Codable {
let givenName: String
let surname: String
let gender: Gender
enum Gender: String, Codable {
case male
case female
}
}
Other nifty stuff includes:
- Automatic handling of optional values
- Support for custom serialization
let json = """
{
"givenName": "Donald",
"surname": "Duck",
"gender": "male"
}
"""
if let person = try? JSONDecoder().decode(Person.self, from: json.data(using: .utf8)!) {
print(person.givenName) // Donald
print(person.surname) // Duck
print(person.gender) // male
}
let person = Person(givenName: "Donald", surname: "Duck", gender: .male)
if let data = try? JSONEncoder().encode(person),
let json = String(data: data, encoding: .utf8) {
print(json) // {"surname":"Duck","givenName":"Donald","gender":"male"}
}
The definition of Codable
looks like this:
public typealias Codable = Decodable & Encodable
Codable
consists of two protocols: Decodable
and Encodable
. By conforming to Codable
, the type will be both encodable and decodable. If specific support for only encoding or decoding is needed, conforming to either Encodable
or Decodable
may make more sense than conforming to Codable
.
To be added.