Giter Site home page Giter Site logo

swift-codable-examples's Introduction

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>

swift-codable-examples's People

Contributors

nathane73 avatar nwdl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.