领域中的双精度嵌套数组 (swift)

Nested array of double in realm (swift)

我有这个JSON:

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

属于另一个 JSON 对象。

尝试用 Realm 和 ObjectMapper 映射它,我发现很难映射 coordinates 属性,这是一个双精度数组。

这就是阅读文档和 S.O 的内容。好像有道理:

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"]

}
}

但是我仍然无法理解如何映射 "coordinates" 对象。请注意,这个问题与 ObjectMapper 本身无关,它更多的是关于如何在 Realm 模型中将 Double 数组分配给 属性 的问题。

我能够按照这个问题中的指示解决这个问题:

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)
        }

    }

}
@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
    }
  }

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

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