如何使用 ObjectMapper 将 [Color] 映射到 [String:Color]
How to map [Color] to [String:Color] using ObjectMapper
我正在尝试将数组类型的 JSON 映射到字典,但我不太确定如何使用 ObjectMapper 进行映射。
示例JSON:
{
"colors": [
{
"id": "red",
"r": "255",
"g": "255",
"b": "255"
}
]
}
您可以执行以下操作。首先将其映射到 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"]
}
}
我正在尝试将数组类型的 JSON 映射到字典,但我不太确定如何使用 ObjectMapper 进行映射。
示例JSON:
{
"colors": [
{
"id": "red",
"r": "255",
"g": "255",
"b": "255"
}
]
}
您可以执行以下操作。首先将其映射到 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"]
}
}