1

我有这个 JSON:

{
"location": {
  "position": {
    "type": "Point",
    "coordinates": [
      45.579553,
      11.751805
    ]
  }
}
}

属于另一个 JSON 对象。

尝试使用 Realm 和 ObjectMapper 映射它,我发现映射作为coordinates双精度数组的属性很困难。

这就是阅读文档的内容,所以似乎很有意义:

import Foundation
import RealmSwift
import ObjectMapper


class Coordinate:Object, Mappable{

dynamic var latitude:Double = 0.0
dynamic var longitude:Double = 0.0

required convenience init?(_ map: Map) {
    self.init()
}
func mapping(map: Map) {
    latitude <- map[""]
    longitude <- map[""]

}


}

class Position: Object, Mappable{

var type:String = ""
var coordinates:Coordinate?

required convenience init?(_ map: Map) {
    self.init()
}
func mapping(map: Map) {
    type <- map["type"]
    coordinates <- map["coordinates"]

}

}

class Location: Object, Mappable{

dynamic var id = ""
dynamic var position:Position?
dynamic var desc = ""

override static func indexedProperties()->[String]{
    return["id"]
}

override class func primaryKey() -> String? {
    return "id"
}


required convenience init?(_ map: Map) {
    self.init()
}

func mapping(map: Map) {
    id <- map["id"]
    position <- map["position"]

}
}

但是我一直在理解如何映射“坐标”对象。请注意,这个问题与 ObjectMapper 本身无关,更多的是关于如何将 Double 数组分配给 Realm 模型中的属性的问题。

4

3 回答 3

2

根据此问题中的指示,我能够解决此问题:

https://github.com/realm/realm-cocoa/issues/1120(学分@jazz-mobility)

class DoubleObject:Object{

    dynamic var value:Double = 0.0

}

class Position: Object, Mappable{

    var type:String = ""
    var coordinates = List<DoubleObject>()

    required convenience init?(_ map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        type <- map["type"]

        var coordinates:[Double]? = nil
        coordinates <- map["coordinates"]


        coordinates?.forEach { coordinate in
            let c = DoubleObject()
            c.value = coordinate
            self.coordinates.append(c)
        }

    }

}
于 2016-05-26T20:04:03.047 回答
1

您现在可以简单地使用List<Double>()而不存储对象。

更多信息可以在这里找到:https ://academy.realm.io/posts/realm-list-new-superpowers-array-primitives/

于 2020-12-21T17:21:35.200 回答
0
@objcMembers class RealmObject: Object, Mappable {

  dynamic var listValues = List<MyRealmObject>()

  required convenience init?(map: Map) {
    self.init()
  }

 // Mappable
 func mapping(map: Map) {
   listValues <- (map["listValues"], RealmlistObjectTransform())
  }

}

@objcMembers class MyRealmObject: Object, Mappable {

    required convenience init?(map: Map) {
      self.init()
    }

    // Mappable
    func mapping(map: Map) {

    }
  }

class RealmlistObjectTransform: TransformType {
    typealias Object = List<MyRealmObject> // My Realm Object here
    typealias JSON = [[String: Any]] // Dictionary here

    func transformFromJSON(_ value: Any?) -> List<MyRealmObject>? {
      let list = List<MyRealmObject>()
      if let actors = value as? [[String: Any]]  {
        let objects = Array<MyRealmObject>(JSONArray: actors)
        list.append(objectsIn: objects)
      }
      return list
    }

    func transformToJSON(_ value: List<MyRealmObject>?) -> [[String: Any]]? {
      if let actors = value?.sorted(byKeyPath: "").toArray(ofType: MyRealmObject.self).toJSON() {
        return actors
      }
      return nil
    }
  }
于 2019-04-17T09:47:29.560 回答