Created
October 2, 2016 22:06
-
-
Save Busta117/1da360581f75f422ac8ce3a6e7d4e1f1 to your computer and use it in GitHub Desktop.
transform array from objectMapper parsing to Realm List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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)!//mapper.map(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