Skip to content

Instantly share code, notes, and snippets.

@Busta117
Created February 15, 2017 19:03
Show Gist options
  • Save Busta117/6233603cbc29b14ae7da20fb8b158d60 to your computer and use it in GitHub Desktop.
Save Busta117/6233603cbc29b14ae7da20fb8b158d60 to your computer and use it in GitHub Desktop.
transform array from objectMapper parsing to Realm List
//
// ObjectMapperRealmHelper.swift
// Pods
//
// Created by Santiago Bustamante on 10/13/16.
//
//
import ObjectMapper
import RealmSwift
//transform array from objectMapper parsing to Realm List
public class ArrayTransform<T:RealmSwift.Object where T:Mappable> : TransformType {
public typealias Object = List<T>
public typealias JSON = Array<Any>
public init(){
}
public func transformFromJSON(_ value: Any?) -> List<T>? {
let result = List<T>()
if let tempArr = value as? Array<Any> {
for entry in tempArr {
let mapper = Mapper<T>()
let model : T = mapper.map(JSONObject: entry)!
result.append(model)
}
}
return result
}
public func transformToJSON(_ value: List<T>?) -> Array<Any>? {
if let value = value, value.count > 0
{
var result = Array<Any>()
for entry in value {
let mapped = Mapper().toJSON(entry)
result.append(mapped)
}
return result
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment