Skip to content

Instantly share code, notes, and snippets.

@haraldmartin
Created January 30, 2017 14:30
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 haraldmartin/4710a47f05264cb171202a8720c1367a to your computer and use it in GitHub Desktop.
Save haraldmartin/4710a47f05264cb171202a8720c1367a to your computer and use it in GitHub Desktop.
JSON array to Realm's List type with ObjectMapper
import Foundation
import ObjectMapper
import RealmSwift
class ListTransform<T:RealmSwift.Object where T:Mappable> : TransformType {
typealias Object = List<T>
typealias JSON = [AnyObject]
let mapper = Mapper<T>()
func transformFromJSON(value: AnyObject?) -> Object? {
var results = List<T>()
if let value = value as? [AnyObject] {
for json in value {
if let obj = mapper.map(json) {
results.append(obj)
}
}
}
return results
}
func transformToJSON(value: Object?) -> JSON? {
var results = [AnyObject]()
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