【问题标题】:How to map a realm list of custom objects using Mappable protocol in Swift如何在 Swift 中使用 Mappable 协议映射自定义对象的领域列表
【发布时间】:2016-11-02 04:46:30
【问题描述】:

在我的领域对象模型中,我有一个名为“事件”的对象。每个 Event 都有一个 EventLocatons 列表。我正在尝试从 json 映射这些对象,但 EventLocations 列表始终为空。 对象看起来像这样(为清楚起见进行了简化):

class Event: Object, Mappable {
    override class func primaryKey() -> String? {
        return "id"
    }

    dynamic var id = "" 
    var eventLocations:List<EventLocation> = List<EventLocation>()

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

class EventLocation: Object, Mappable {
    override class func primaryKey() -> String? {
        return "id"
    }

    dynamic var id: String = ""
    dynamic var name: String = ""

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

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

我拥有的 json 是一个 Event 对象数组。它来自 Alamofire 响应,我将其映射为:

var events = Mapper<Event>().mapArray(json!)

json 看起来像这样:

[
  {
    "id" : "21dedd6d",
    "eventLocations" : [
      {
        "name" : "hh",
        "id" : "e18df48a",
       },
      {
        "name" : "tt",
        "fileId" : "be6116e",
      }
    ]
  },
  {
    "id" : "e694c885",
    "eventLocations" : [
      {
        "name" : "hh",
        "id" : "e18df48a",
       },
      {
        "name" : "tt",
        "fileId" : "be6116e",
      }
    ]
  }
 ]

有谁知道如何使用 Mappable 协议映射自定义对象列表。为什么“eventLocations”列表总是空的?

【问题讨论】:

    标签: json swift realm objectmapper


    【解决方案1】:

    看看one of the Issues page on ObjectMapper's GitHub repo,似乎还没有正确支持Realm List对象。

    该问题还列出了暂时使其正常工作的潜在解决方法,我将在此处反映:

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

    【讨论】:

    • 这是最好的解决方案,领域使用托管,然后不能声明类型为 var 的列表,总是使用 let!谢谢!
    【解决方案2】:

    另一种解决方案是为 ObjectMapper 实现自定义转换。 你可以找到一个实现here

    然后在你的代码中:

    eventLocations <- (map["eventLocations"], ListTransform<EventLocation>())
    

    【讨论】:

      【解决方案3】:

      您可以为此添加一个运算符。

      Swift 3 实现:

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

      现在您不需要任何额外的代码或转换。

      list &lt;- map["name"]

      我已经创建了一个要点。请检查https://gist.github.com/danilValeev/ef29630b61eed510ca135034c444a98a

      【讨论】:

      • 这是最好的答案。我不知道为什么人们没有为你点击 UP。
      猜你喜欢
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 2016-05-26
      相关资源
      最近更新 更多