Swift examples

Simple model

A simple model with some scalar properties.
{
  "definitions": {
    "Student": {
      "description": "A simple student struct",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  },
  "root": "Student"
}

Student.swift

// A simple student struct
class Student: Codable {
    var firstName: String
    var lastName: String
    var age: Int

    enum CodingKeys: String, CodingKey {
        case firstName = "firstName"
        case lastName = "lastName"
        case age = "age"
    }
}

Model with inheritance

A student struct which extends from a different struct.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    },
    "Student": {
      "parent": {
        "type": "reference",
        "target": "Human"
      },
      "type": "struct",
      "properties": {
        "studentId": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

Human.swift

class Human: Codable {
    var firstName: String
    var lastName: String
    var age: Int

    enum CodingKeys: String, CodingKey {
        case firstName = "firstName"
        case lastName = "lastName"
        case age = "age"
    }
}

Student.swift

class Student: Human {
    var studentId: String

    enum CodingKeys: String, CodingKey {
        case studentId = "studentId"
    }
}

Model with reference

A student struct which reference a faculty struct.
{
  "definitions": {
    "Student": {
      "description": "A student struct with an assigned faculty",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "faculty": {
          "type": "reference",
          "target": "Faculty"
        }
      }
    },
    "Faculty": {
      "type": "struct",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

Student.swift

// A student struct with an assigned faculty
class Student: Codable {
    var firstName: String
    var lastName: String
    var age: Int
    var faculty: Faculty

    enum CodingKeys: String, CodingKey {
        case firstName = "firstName"
        case lastName = "lastName"
        case age = "age"
        case faculty = "faculty"
    }
}

Faculty.swift

class Faculty: Codable {
    var name: String

    enum CodingKeys: String, CodingKey {
        case name = "name"
    }
}

Map with string values

A student struct which contains a map with arbitrary string values.
{
  "definitions": {
    "Student": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "properties": {
          "type": "map",
          "schema": {
            "type": "string"
          }
        }
      }
    }
  },
  "root": "Student"
}

Student.swift

class Student: Codable {
    var firstName: String
    var lastName: String
    var age: Int
    var properties: Dictionary<String, String>

    enum CodingKeys: String, CodingKey {
        case firstName = "firstName"
        case lastName = "lastName"
        case age = "age"
        case properties = "properties"
    }
}

Model with discriminator

A struct which uses a discriminator mapping.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "location": {
          "type": "reference",
          "target": "Location"
        }
      }
    },
    "Location": {
      "type": "struct",
      "base": true,
      "properties": {
        "type": {
          "type": "string"
        }
      },
      "discriminator": "type",
      "mapping": {
        "Web": "web",
        "World": "world"
      }
    },
    "Web": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "url": {
          "type": "string"
        }
      }
    },
    "World": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "lat": {
          "type": "string"
        },
        "long": {
          "type": "string"
        }
      }
    }
  },
  "root": "Human"
}

Human.swift

class Human: Codable {
    var firstName: String
    var lastName: String
    var location: Location

    enum CodingKeys: String, CodingKey {
        case firstName = "firstName"
        case lastName = "lastName"
        case location = "location"
    }
}

Location.swift

class Location: Codable {
    var _type: String

    enum CodingKeys: String, CodingKey {
        case _type = "type"
    }
}

Web.swift

class Web: Location {
    var url: String

    enum CodingKeys: String, CodingKey {
        case url = "url"
    }
}

World.swift

class World: Location {
    var lat: String
    var long: String

    enum CodingKeys: String, CodingKey {
        case lat = "lat"
        case long = "long"
    }
}

Model with generics

A struct which uses generics.
{
  "definitions": {
    "Student": {
      "type": "object",
      "properties": {
        "matricleNumber": {
          "type": "integer"
        }
      }
    },
    "StudentMap": {
      "type": "struct",
      "parent": {
        "type": "reference",
        "target": "Map",
        "template": {
          "T": "Student"
        }
      }
    },
    "Map": {
      "type": "object",
      "properties": {
        "totalResults": {
          "type": "integer"
        },
        "entries": {
          "type": "array",
          "schema": {
            "type": "generic",
            "name": "T"
          }
        }
      }
    }
  },
  "root": "StudentMap"
}

Student.swift

class Student: Codable {
    var matricleNumber: Int

    enum CodingKeys: String, CodingKey {
        case matricleNumber = "matricleNumber"
    }
}

Map.swift

class Map<T>: Codable {
    var totalResults: Int
    var entries: Array<T>

    enum CodingKeys: String, CodingKey {
        case totalResults = "totalResults"
        case entries = "entries"
    }
}

StudentMap.swift

class StudentMap: Map<Student> {

    enum CodingKeys: String, CodingKey {
    }
}

Import other TypeSchema specification

A struct which references an external TypeSchema.
{
  "import": {
    "my_ns": "file:///generic.json"
  },
  "definitions": {
    "Faculty": {
      "type": "object",
      "properties": {
        "description": {
          "type": "string"
        },
        "students": {
          "type": "array",
          "schema": {
            "type": "reference",
            "target": "my_ns:StudentMap"
          }
        }
      }
    }
  },
  "root": "Faculty"
}

Faculty.swift

class Faculty: Codable {
    var description: String
    var students: Array<StudentMap>

    enum CodingKeys: String, CodingKey {
        case description = "description"
        case students = "students"
    }
}

Edit this page