Realm To-Many Relationships types is List. and ObjectMapper is Array OR Set.
ObjectMapper could support RealmSwift List types?
I have the same problem.
Currently I'm trying the following workaround. I wonder if you have any other ideas until the problem is fixed?
class MyObject: Object, Mappable {
let tags = List<Tag>()
required convenience init?(_ map: Map) { self.init() }
func mapping(map: Map) {
var tags: [Tag]?
tags <- map["tags"]
if let tags = tags {
for tag in tags {
self.tags.append(tag)
}
}
}
}
same for me
You can use ArrayTransform
items <- (map["items"],ArrayTransform<Item>())
There is reference to simple extension that can be used https://github.com/Hearst-DD/ObjectMapper#objectmapper--realm
I guess this issue should be closed. @pawin s solution works perfectly
I've created an operator which properly fixes this issue. Please, check https://gist.github.com/danilValeev/ef29630b61eed510ca135034c444a98a
With this operator you don't need any additional code or transforms.
list <- map["name"]
Here is Swift 3 version. To see the version for Swift 2 and an example of usage please check the gist.
import Foundation
import RealmSwift
import ObjectMapper
infix operator <-
/// Object of Realm's List type
public func <- <T: Mappable>(left: List<T>, right: Map) {
var array: [T]?
if right.mappingType == .toJSON {
array = Array(left)
}
array <- right
if right.mappingType == .fromJSON {
if let theArray = array {
left.append(objectsIn: theArray)
}
}
}
Most helpful comment
You can use ArrayTransform
items <- (map["items"],ArrayTransform<Item>())