SwiftyModel

How code is generated

How SwiftyModel infers types, names properties, and handles arrays and nesting. Every example on this page was generated by SwiftyModel with its default settings.

Basic types

JSON valueSwift typeRead with
StringStringstringValue
Whole numberIntintValue
Decimal numberDoubledoubleValue
true / falseBoolboolValue
ObjectA nested typeType(json:)
nullAny, or JSON with Codable onobject / the raw JSON

Properties are listed in alphabetical order of their JSON keys.

Nested objects

Each nested object gets a nested type named after its key in UpperCamelCase: profile becomes Profile, and house_name becomes HouseName. Nested types are declared inside the type that contains them.

Objects are expanded up to 10 levels deep. Anything deeper is kept as JSON rather than given its own type, so no data is lost.

Arrays

  • When every element has the same type, you get a typed array such as [String].
  • Multi-dimensional arrays are typed at every level, such as [[Int]].
  • A mix of whole and decimal numbers becomes [Double].
  • When every object in an array has the same keys and value types, they share one element type, named after the array's key: items becomes Items.
  • When the element types differ, or the objects have different shapes, the array is [Any], or [JSON] with Codable on.
JSON
{
  "matrix": [[1, 2], [3, 4]],
  "scores": [1, 2.5],
  "mixed": [1, "a"],
  "items": [{ "id": 1 }, { "id": 2 }],
  "note": null
}
Swift
import Foundation
import SwiftyJSON

struct Report {
    struct Items {
        let id: Int

        init(json: JSON) {
            id = json["id"].intValue
        }
    }

    let items: [Items]
    let matrix: [[Int]]
    let mixed: [Any]
    let note: Any
    let scores: [Double]

    init(json: JSON) {
        items = json["items"].arrayValue.map { Items(json: $0) }
        matrix = json["matrix"].arrayValue.map { $0.arrayValue.map { $0.intValue } }
        mixed = json["mixed"].arrayValue.map { $0.object }
        note = json["note"].object
        scores = json["scores"].arrayValue.map { $0.doubleValue }
    }
}

With Codable on, values whose type cannot be pinned down use SwiftyJSON's JSON type, because Any cannot conform to Codable:

Swift · Codable
import Foundation
import SwiftyJSON

struct Report: Codable {
    let mixed: [JSON]
    let note: JSON

    private enum CodingKeys: String, CodingKey {
        case mixed
        case note
    }

    init(json: JSON) {
        mixed = json["mixed"].arrayValue.map { $0 }
        note = json["note"]
    }
}

When the JSON starts with an array

When the top level is an array of objects, Model Name names the element type. For an array of two { "id": …, "name": … } objects and the model name Item:

Swift
import Foundation
import SwiftyJSON

struct Item {
    let id: Int
    let name: String

    init(json: JSON) {
        id = json["id"].intValue
        name = json["name"].stringValue
    }
}

To read the whole array, call Item(json:) for each element: json.arrayValue.map { Item(json: $0) }.

When the top level is an array of plain values, you get a type alias. For [1, 2, 3] and the model name Scores:

Swift
import Foundation
import SwiftyJSON

typealias Scores = [Int]

Property names

  • camelCase: user_name becomes userName (you can turn this off in Settings).
  • Keywords: keys that are Swift keywords get a prefix or suffix, key as a prefix by default, so class becomes keyClass.
  • Leading digits: keys such as 2nd_place always get the prefix.
  • Clashes: when two keys turn into the same property name, the key that was already spelled that way keeps it and the other is numbered, such as userName2.
  • Other characters: spaces, hyphens and other characters not allowed in Swift names are removed or converted.

However a property is renamed, it is always read from its original JSON key, and with Codable on, CodingKeys map it back to that key too.

JSON
{
  "self": true,
  "class": "A",
  "2nd_place": "Bob",
  "userName": "Ada",
  "user_name": "ada"
}
Swift
import Foundation
import SwiftyJSON

struct Player {
    let key2NdPlace: String
    let keyClass: String
    let keySelf: Bool
    let userName: String
    let userName2: String

    init(json: JSON) {
        key2NdPlace = json["2nd_place"].stringValue
        keyClass = json["class"].stringValue
        keySelf = json["self"].boolValue
        userName = json["userName"].stringValue
        userName2 = json["user_name"].stringValue
    }
}