I am trying to transform "Object Mapper" to "Codable". My response which comes from service includes NSArray
which includes custom objects. I need to use NSArray
and NSDictionary
in Codable Class. But, i failed.I tried to use third party library like AnyCodable, but, i failed again. I can't change the response on server side. It must come as Array. I must use Array. Do you have any suggestion or information?
class Person : Codable { //Error 1
var name: String?
var data: NSArray?
private enum CodingKeys : String, CodingKey {
case name
case data
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(data, forKey: .data) //Error 2
}
func decode(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.data = try container.decode(NSArray.self, forKey: .data) //Error 3
}
}
Error 1: "Type 'Person' does not conform to protocol 'Decodable'"
Error 2: "Argument type 'NSArray' does not conform to expected type 'Encodable'"
Error 3: "Instance method 'decode(_:forKey:)' requires that 'NSArray' conform to 'Decodable'"
Sample response is here.All items in the array don't have the same contents. Every item in array has different type.
{
"data": [
{
"languageCode": "EN",
"deviceInformation": {
"screenSize": "height:812.0 width:375.0",
"connectionType": "wifi",
"deviceType": "iPhone",
"deviceCode": "D01D304C-D05C-4443-9A92-031C55D14XC7",
"operatingSystemVersion": "12.2",
"applicationVersion": "1.0"
},
"lastUpdatedParamDate": "13.05.2019 14:44:24",
"skipOptionalUpdate": 0
}
]
}