簡體   English   中英

領域中的 double 嵌套數組(swift)

[英]Nested array of double in realm (swift)

我有這個 JSON:

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

屬於另一個 JSON 對象。

試圖用 Realm 和 ObjectMapper 映射它,我發現映射coordinates屬性是一個雙精度數組的困難。

這就是閱讀文檔和 SO 似乎有意義的內容:

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 模型中的屬性的問題。

我能夠按照此問題中的指示解決此問題:

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

    }

}

您現在可以簡單地使用List<Double>()而無需存儲對象。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM