Skip to content

Instantly share code, notes, and snippets.

@RomanVolkov
Created January 30, 2017 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RomanVolkov/b7c603bf72c74c8885270c093e4958c6 to your computer and use it in GitHub Desktop.
Save RomanVolkov/b7c603bf72c74c8885270c093e4958c6 to your computer and use it in GitHub Desktop.
Convert ObjectMapper array to Realm's List<> type
import Foundation
import ObjectMapper
import RealmSwift
final class ListTransform<T:Object> : TransformType where T:Mappable {
typealias Object = List<T>
typealias JSON = [[String:Any]]
let mapper = Mapper<T>()
func transformFromJSON(_ value: Any?) -> List<T>? {
let result = List<T>()
if let tempArr = value as? [Any] {
for entry in tempArr {
let mapper = Mapper<T>()
let model : T = mapper.map(JSONObject: entry)!
result.append(model)
}
}
return result
}
func transformToJSON(_ value: Object?) -> JSON? {
var results = [[String:Any]]()
if let value = value {
for obj in value {
let json = mapper.toJSON(obj)
results.append(json)
}
}
return results
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment