Skip to content

Getting Started

Anton edited this page Jun 3, 2017 · 3 revisions

Once configured per the Installation instructions, run the build script once ⌘+B. You will see the Model folder appears within your project.

All JSON Mappings will go into the Model/Mappings directory, and your Classes will be generated into the Model directory, then automatically added to your project during build time.

Simple Example

Let look as a simple mapping that defines a class below in JSON.

Input File: JSOModelKit/Model/Mappings/Business.json

{
    "uuid" : {
        "key" : "identifier",
        "type" : "Double",
        "nonoptional" : "true"
    },
    "businessName" : {
        "key" : "business_name",
        "type" : "String"
    },
    "ratings" : {
        "key" : "ratings",
        "type" : "Array",
        "subtype" : "Double"
    },
    "metaTags" : {
        "key" : "metadata.tags",
        "type" : "Array",
        "subtype" : "String"
    },
    "open" : {
        "key" : "open",
        "type" : "Bool",
        "default" : "0"
    }
}

Run the build script once ⌘+B. and you will see that it generated the following files in the output directory. This will also be reflected in the Project structure within the Model group

NOTE: Every time a new mapping configurations is added, the following build will always be canceled by Xcode, and needs to be run again. This is due to the project file changing in the middle of a build, since a new file is added. If no new mapping is added, it will build as usual

Observe the internal file that was generated

class _Business  {

	var uuid : Double

	var metaTags : [Float]?
	var ratings : [Float]?
	var locations : [Coordinate]?
	var dateOpened : Date?
	var businessName : String?

	..........

	required init(uuid  _uuid : Double)  {
		// Required init with all non-optionals defined
	}

	convenience init?(_ dictionary: Dictionary<String, AnyObject>) {
		// Failable initializer, returns nil when any non-optional values is not defined
	}

	func updateWithDictionary(dictionary: Dictionary<String, AnyObject>) {
		// Helper methods to updated an instance with a new dictionary of values
	}
}

Once a JSON response is received call the following method, and all the properties will be parsed and mapped accordingly.

let newInstance = TestModelObject(dataDictionary)