-
Notifications
You must be signed in to change notification settings - Fork 2
Struct Transformations
To map enums use the JMTransformerProtocol
. Let's look at a dictionary for a business object, and see how we can map an enum to represent a struct for the coordinates:
{
'business_uuid' : 9223123456754775807,
'business_name' : 'NYC Restaurant',
'business_longitude' : 40.7053319,
'business_latitude' : -74.0129945,
}
Unlike the model objects, JSONModelKit does not autogenerate structs, due to the lack of inheritance. Structs would be un-customizable if it did, and would prevent the developer from using the many features Swift offers with structs. For the purposes of this example, let assume that we created a struct to represent the coordinates for our custom Business Object.
struct Coordinate {
var longitude: Double
var latitude: Double
}
Once we have defined a Coordinate struct, let's create a mapper that parses out the values from the response and return the value of Coordinate type.
let longitudeKey = "business_longitude"
let latitudeKey = "business_latitude"
public class CoordinateTransformer : JMTransformerProtocol {
public func transformValues(inputValues : Dictionary<String, Any>?) -> Any? {
if let coordinateDictionary = inputValues as? Dictionary<String, Double>,
let longitude = coordinateDictionary[longitudeKey],
let latitude = coordinateDictionary[latitudeKey] {
return Coordinate(longitude : longitude, latitude : latitude)
}
}
}
return nil
}
}
Now that we have created a transformer, let's create some mapping for our business Object:
/Model/Mappings/Business.json | PLIST Equivalent
{
"uuid" : { ... },
"fullName" : {
"transformer" : "CoordinateTransformer",
"type" : "Coordinate",
"key" : [
"business_longitude",
"business_latitude"
]
}
}
After the creation of the mapping, perform a build (⌘-B). The changes should be reflected accordingly in the internal _Business.swift
class.
import Foundation
import JSONModelKit
class _Business {
var uuid : Double?
var name : String?
var coordinates : Coordinates?
required init() {...}
convenience init?(_ dictionary: Dictionary<String, Any>) {...}
}
After calling the fail-able initializer - or udpateWithDictionary method with a dictionary representation - JSONModelKit will use the custom transformer to map the struct accordingly.
Note: The the keys defined in the property mapping correspond to the keys in the dictionary of values passed to the public func transformValues(inputValues : Dictionary<String, Any>?) -> Any?
method defined by the protocol.
A Good Day Production
LinkedIn | Twitter @AntonTheDev