Skip to content

detroit-labs/swift-codable-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift Codable Examples

Codable was implemented in Swift 4, and made available starting in Xcode 9.0.

For more history details, see swift evolution: SE-0166

Codable

public typealias Codable = Decodable & Encodable

Decodable

public protocol Decodable {
    public init(from decoder: Decoder) throws
}

Encodable

public protocol Encodable {
    public func encode(to encoder: Encoder) throws
}

Supported File Formats

JSON Examples

Simple

{
  "first": "Steve",
  "last": "Dave"
}
{
  "name": "Gizmo",
  "kind": "dog",
  "age": 14,
  "isFriendly": true
}

Nested

{
  "age": 45,
  "name": {
    "first": "Steve",
    "last": "Dave"
  }
}

Array

{
  "strings": ["Red", "Green", "Blue"],
  "numbers": [1, 3, 5, 7, 9],
  "bools": [true, true, false]
}

Common

{
  "first-name": "Steve",
  "last-name": "Dave"
}

Tuple

{
  "dimension": [ 16, 9 ]
}

Hierarchy

{
  "shipping-address": {
    "name": "Detroit Labs",
    "city": "Detroit",
    "state": "MI"
  },
  "billing-address": {
    "name": "Paula Goodski",
  },
  "items": [
    { "sku": "I001", "name": "Labs Beats V1", "quantity": 10000 }
  ]
}

Inheritance

{
  "cat": {
    "name": "Fluffy",
    "lenghtOfTail": 6
  },
  "dog": {
    "name": "Skippy",
    "numberOfBones": 1
  }
}

Option Set

{
  "sports": [ "swimming", "running" ]
}

Inconvenient Array

{
  "item.3": "Delta",
  "item.0": "Alfa",
  "item.2": "Charlie",
  "item.1": "Bravo"
}

Date With Time (ISO8601)

{
  "origin": "Atlanta, GA",
  "departure": "2019-03-28T08:15:02Z",
  "destination": "Detroit, MI",
  "arrival": "2019-04-01T18:45:20Z"
}

Date Without Time

{
  "origin": "Atlanta, GA",
  "departure": "2019-03-28",
  "destination": "Detroit, MI",
  "arrival": "2019-04-01"
}

Empty Collection

{
  "mother": {
    "first-name": "Anne",
    "last-name": "Dave"
  },
  "father": {
    "first-name": "Steve",
    "last-name": "Dave"
  },
  "brother": {
  }
}

Enum with Associated Values

[
  {
    "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"]
    }
  }
]

Safe Decodable

[
  {
    "sku": "I001",
    "name": "Labs Beats V1",
  },
  {
    "sku": "I002",
    "name": "Labs Beats V2",
    "quantity": 8
  }
]

Decode Shorthand

{
  "name": "Office Space",
  "address": "123 Main St"
}

Property List Examples

Simple

<?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>

Nested

<?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>

Releases

No releases published

Packages

No packages published