Created
January 30, 2017 11:06
-
-
Save RomanVolkov/b7c603bf72c74c8885270c093e4958c6 to your computer and use it in GitHub Desktop.
Convert ObjectMapper array to Realm's List<> type
This file contains 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
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