Codable was implemented in Swift 4, and made available starting in Xcode 9.0.
For more history details, see swift evolution: SE-0166
public typealias Codable = Decodable & Encodable
public protocol Decodable {
public init(from decoder: Decoder) throws
}
public protocol Encodable {
public func encode(to encoder: Encoder) throws
}
{
"first": "Steve",
"last": "Dave"
}
- Implementation: Name.swift, AnotherName.swift
- Tests: NameTests.swift, AnotherNameTests.swift
{
"name": "Gizmo",
"kind": "dog",
"age": 14,
"isFriendly": true
}
- Implementation: Pet.swift
- Tests: PetTests.swift
{
"age": 45,
"name": {
"first": "Steve",
"last": "Dave"
}
}
- Implementation: Person.swift
- Tests: PersonTests.swift
{
"strings": ["Red", "Green", "Blue"],
"numbers": [1, 3, 5, 7, 9],
"bools": [true, true, false]
}
- Tests: ArrayTests.swift, NameTests.swift
{
"first-name": "Steve",
"last-name": "Dave"
}
- Implementation: Contact.swift
- Tests: ContactTests.swift
{
"dimension": [ 16, 9 ]
}
- Implementation: Size.swift
- Tests: SizeTests.swift
{
"shipping-address": {
"name": "Detroit Labs",
"city": "Detroit",
"state": "MI"
},
"billing-address": {
"name": "Paula Goodski",
},
"items": [
{ "sku": "I001", "name": "Labs Beats V1", "quantity": 10000 }
]
}
- Implementation: Invoice.swift, Address.swift, Item.swift
- Tests: InvoiceTests.swift
{
"cat": {
"name": "Fluffy",
"lenghtOfTail": 6
},
"dog": {
"name": "Skippy",
"numberOfBones": 1
}
}
- Implementation: Animal.swift, Cat.swift, Dog.swift
- Tests: AnimalTests.swift, CatTests.swift, DogTests.swift
{
"sports": [ "swimming", "running" ]
}
- Implementation: Sports.swift
- Tests: SportsTests.swift
{
"item.3": "Delta",
"item.0": "Alfa",
"item.2": "Charlie",
"item.1": "Bravo"
}
- Implementation: InconvenientArray.swift
- Tests: InconvenientArrayTests.swift
{
"origin": "Atlanta, GA",
"departure": "2019-03-28T08:15:02Z",
"destination": "Detroit, MI",
"arrival": "2019-04-01T18:45:20Z"
}
- Implementation: Trip.swift
- Tests: TripTests.swift
{
"origin": "Atlanta, GA",
"departure": "2019-03-28",
"destination": "Detroit, MI",
"arrival": "2019-04-01"
}
- Implementation: Trip.swift
- Tests: TripTests.swift
{
"mother": {
"first-name": "Anne",
"last-name": "Dave"
},
"father": {
"first-name": "Steve",
"last-name": "Dave"
},
"brother": {
}
}
- Resources: NilOnEmpty.swift, RawCodingKeys.swift
- Implementation: AnotherFamily.swift, CorruptFamily.swift, Family.swift
- Tests: AnotherFamilyTests.swift, CorruptFamilyTests.swift
[
{
"bicycle": {}
},
{
"boat": {
"floats": true
}
},
{
"car": {
"year": 2016,
"make": "Tesla",
"model": "Model S"
}
},
{
"limo": {
"_0": "Black",
"something": true,
"_2": 8
}
},
{
"truck": {
"wheels": 18
}
},
{
"van": {
"contents": ["Wrench", "Hammer"]
}
}
]
- Resources: Vehicle.swift
- Tests: VehicleTests.swift
[
{
"sku": "I001",
"name": "Labs Beats V1",
},
{
"sku": "I002",
"name": "Labs Beats V2",
"quantity": 8
}
]
- Resources: SafeDecodable.swift
- Tests: SafeDecodableTests.swift
{
"name": "Office Space",
"address": "123 Main St"
}
- Resources: KeyedDecodingContainer.swift
- Implementation: Building.swift
- Tests: BuildingTests.swift
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>first</key>
<string>Steve</string>
<key>last</key>
<string>Dave</string>
</dict>
</plist>
- Implementation: Name.swift
- Tests: NameTests.swift
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>age</key>
<integer>45</integer>
<key>name</key>
<dict>
<key>first</key>
<string>Steve</string>
<key>last</key>
<string>Dave</string>
</dict>
</dict>
</plist>
- Implementation: Person.swift
- Tests: PersonTests.swift