Skip to content

Struct Transformations

Anton edited this page Jul 16, 2017 · 6 revisions

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:

Response Dictionary

{
    '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 Definition

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.

JMTransformerProtocol Implementation

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
    }
}

Mapping

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"
	]
    }
}

Model

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.

Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy