Need help with ModelRocket?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

452 Stars 20 Forks MIT License 38 Commits 0 Opened issues


An iOS framework for creating JSON-based models. Written in Swift.

Services available


Need anything else?

Contributors list


Build Status Carthage compatible CocoaPods Compatible License Platform

An iOS framework for creating JSON-based models. Written in Swift (because it totally rules!)


  • iOS 8.0+
  • Xcode 7.3
  • Swift 2.2


Embedded frameworks require a minimum deployment target of iOS 8


Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following commands:

$ brew update
$ brew install carthage

To integrate ModelRocket into your Xcode project using Carthage, specify it in your Cartfile:

github "ovenbits/ModelRocket"

Then, run

carthage update

Follow the current instructions in Carthage's README for up-to-date installation instructions.


CocoaPods is a dependency manager for Cocoa projects.

CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:

$ gem install cocoapods

To integrate ModelRocket into your Xcode project using CocoaPods, specify it in your Podfile:

source ''
platform :ios, '8.0'

pod 'ModelRocket'

Then, run

pod install

Swift Package Manager

The Swift Package Manager is a dependency management tool provided by Apple, still in early design and development. For more infomation check out its GitHub Page.

You can use the Swift Package Manager to install

by adding it as a dependency in your
file: ```swift import PackageDescription

let package = Package( name: "PROJECT_NAME", targets: [], dependencies: [ .Package(url: "", versions: "1.2.3" ..< Version.max) ] ) ```


Creating a custom object

class Vehicle: Model {
    let make  = Property(key: "make")
    let model = Property(key: "model", required: true)
    let year  = Property(key: "year") { year in
        if year < 2015 {
            // offer discount
    let color = Property(key: "color", defaultValue: UIColor.blackColor())

NOTE: As with all Swift variables,

should always be used, unless
is absolutely needed. In the case of Model objects,
should be used for all
properties, as it still allows the underlying
to be changed, unless you truly need to reassign the property

Supported Types

  • String
  • Bool
  • Int
  • UInt
  • Double
  • Float

In addition to the core types above, ModelRocket also supports serialization for several other classes out of the box:

  • NSDate
    — ISO8601-formatted string (
  • UIColor
    — hex-color string (
    — any url string (
  • NSNumber
    — any number, can be used in place of
    , and

Creating an object with a typed array

// `Model` subclasses get `fromJSON` and `toJSON` implementations on `JSONTransformable` for free,
// but explicit `JSONTransformable` conformance is still required
extension Vehicle: JSONTransformable {}

class Vehicles: Model { let vehicles = PropertyArray(key: "vehicles") }

conforms to
, therefore, the
syntax is not necessary when iterating through the values. For example:
let allVehicles = Vehicles(json: )

// using .values syntax for vehicle in allVehicles.vehicles.values { }

// using CollectionType conformance for vehicle in allVehicles.vehicles { }

Creating an object with a typed dictionary

class Car: Vehicle {
    let purchasedTrims = PropertyDictionary(key: "purchased_trims")

conforms to
, therefore, the
syntax is not necessary when iterating through the keys and values. For example:
let vehicle = Vehicle(json: )

// using .values syntax for (key, trim) in vehicle.purchasedTrims.values { }

// using CollectionType conformance for (key, trim) in vehicle.purchasedTrims { }

NOTE: All object in the dictionary must be of the same type. If they're not, the app won't crash, but values of different types will be discarded

Initializing and using a custom object

// instantiate object
let vehicle = Vehicle(json: json)

// get property type println("Vehicle make property has type: (vehicle.make.type)")

// get property value if let make = vehicle.make.value { println("Vehicle make: (make)") }

Model objects also contain a failable initializer, which will only return an initialized object if all properties marked as

required = true
are non-nil.
// instantiate object, only if `json` contains a value for the `make` property
if let vehicle = Vehicle(strictJSON: json) {
    // it's best to avoid implicitly unwrapped optionals, however, since `vehicle` is initialized iff `make` is non-nil, if can be force-unwrapped safely here
    println("Vehicle make: \(vehicle.make.value!)")
else {
    pintln("Invalid JSON")

Subclassing a custom object

class Car: Vehicle {
    let numberOfDoors = Property(key: "number_of_doors")

Adding a custom object as a property of another object

The custom object must conform to the JSONTransformable protocol by defining the following variables/functions

  • class func fromJSON(json: JSON) -> T?
  • func toJSON() -> AnyObject
class Vehicle: Model {
    let manufacturer = Property(key: "manufacturer")

class Manufacturer: Model { let companyName = Property(key: "company_name") let headquarters = Property(key: "headquarters") let founded = Property(key: "founded") }

extension Manufacturer: JSONTransformable { class func fromJSON(json: JSON) -> Manufacturer? { return Manufacturer(json: json) } func toJSON() -> AnyObject { return self.json().dictionary } }

Using an enum as a property

ModelRocket supports enum types for

properties, as long as the enum conforms to the

As a simple example, the material type of a vehicle's interior could use an enum like this:

enum VehicleInterior: String {
    case Fabric = "fabric"
    case Leather = "leather"

extension VehicleInterior: JSONTransformable { static func fromJSON(json: JSON) -> VehicleInterior? { return VehicleInterior(rawValue: json.stringValue) } func toJSON() -> AnyObject { return rawValue } }

class Vehicle: ModelRocket { let interior = Property(key: "interior") }



closure (also available on
) provides a mechanism for work to be done after all properties of a
object have been initialized from JSON but before the
object has finished initializing.
class Vehicles: Model {
    let vehicles = PropertyArray(key: "vehicles") { (values) -> Void in
        for vehicle in values {
            println("postHook vehicle: \(vehicle.make.value!)")

accessor usage pattern

A ModelRocket property is of type

. When accessing the property's value, you go through
, e.g.:
let vehicleMake = make.value

It is perfectly acceptable to to utilize

s and access the property
directly. However, you may want a different public API for your model objects.
private let _make = Property(key: "make")
public var make: String {
  get {
    return make.value ?? "unknown make"
  set {
    make.value = newValue

This usage pattern enables:

  • A cleaner public API
  • A public API that makes the type more proper: we expect "make" to be a string, not a
  • value
    is optional because it must be for general applicability, but your API may be more correct to have non-optional. Of course, if your API wants an optional, that's fine too.
  • The ability to process or convert the raw JSON value to other values more proper to your object's public API
  • Whereas a
    could be set, you could omit the set accessor for a read-only property, again helping to expose exactly the API for your object that you desire.
  • This usage of the bridge pattern enables ModelRocket to become an implementation detail and minimize dependencies and long-term maintenance.

Implementing a class cluster

Override the

modelForJSON(json: JSON) -> Model
class Vehicle: Model {
    let make = Property(key: "make")
    let model = Property(key: "model")
    let year = Property(key: "year")
    let color = Property(key: "color")
    let manufacturer = Property(key: "manufacturer")

override class func modelForJSON(json: JSON) -&gt; Vehicle {

    switch json["type"].stringValue {
    case "car":
        return Car(json: json)
    case "plane":
        return Plane(json: json)
    case "bike":
        return Bike(json: json)
        return Vehicle(json: json)


Then to access subclass-specific properties, use a switch-case

let vehicle = Vehicle.modelForJSON(vehicleJSON)

switch vehicle { case let car as Car: // drive the car case let plane as Plane: // fly the plane case let bike as Bike: // ride the bike default: // do nothing }

Obtaining the object's JSON representation

Calling the

function on a Model subclass returns a tuple containing:
  • dictionary: [String : AnyObject]
  • json: JSON
  • data: NSData

Obtaining a copy of a custom object


on the object, and cast to the correct type. Example:
let vehicleCopy = vehicle.copy() as! Vehicle


ModelRocket is released under the MIT license. See LICENSE for details.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.