【问题标题】:How to map [Color] to [String:Color] using ObjectMapper如何使用 ObjectMapper 将 [Color] 映射到 [String:Color]
【发布时间】:2017-07-26 06:47:22
【问题描述】:

我正在尝试将 Array 类型的 JSON 映射到 Dictionary,但我不太确定如何使用 ObjectMapper。

示例 JSON:

{ 
  "colors": [
   { 
    "id": "red",
    "r": "255",
    "g": "255",
    "b": "255"
   }
  ]
}

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: ios swift objectmapper


【解决方案1】:

您可以执行以下操作。首先将其映射到array,然后使用didSet 将其映射到字典。

class MyClass: Mappable {
    private var arrayColors = [MyClass2] {
    didSet {
            var mapTypes = [String:MyClass2]?
            for obj in arrayColors {
                mapTypes[obj.id] = obj
            }

            types = mapTypes
        }
    }

    var colors:[String:MyClass2] = [String:MyClass2]()

    func mapping(map: Map) {
        arrayColors <- map["colors"]
    }
}

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多