I have to decode a JSON with a big structure and a lot of nested arrays. I have reproduced the structure in my UserModel file, and it works, except with one property (postcode) that is in a nested array (Location) that sometimes is an Int and some other is a String. I don't know how to handle this situation and tried a lot of different solutions. The last one I've tried is from this blog https://agostini.tech/2017/11/12/swift-4-codable-in-real-life-part-2/ And it suggests using generics. But now I can't initialize the Location object without providing a Decoder():
Any help or any different approach would be appreciated. The API call is this one: https://api.randomuser.me/?results=100&seed=xmoba This is my UserModel File:
import Foundation
import UIKit
import ObjectMapper
struct PostModel: Equatable, Decodable{
    static func ==(lhs: PostModel, rhs: PostModel) -> Bool {
        if lhs.userId != rhs.userId {
            return false
        }
        if lhs.id != rhs.id {
            return false
        }
        if lhs.title != rhs.title {
            return false
        }
        if lhs.body != rhs.body {
            return false
        }
        return true
    }
    var userId : Int
    var id : Int
    var title : String
    var body : String
    enum key : CodingKey {
        case userId
        case id
        case title
        case body
    }
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: key.self)
        let userId = try container.decode(Int.self, forKey: .userId)
        let id = try container.decode(Int.self, forKey: .id)
        let title = try container.decode(String.self, forKey: .title)
        let body = try container.decode(String.self, forKey: .body)
        self.init(userId: userId, id: id, title: title, body: body)
    }
    init(userId : Int, id : Int, title : String, body : String) {
        self.userId = userId
        self.id = id
        self.title = title
        self.body = body
    }
    init?(map: Map){
        self.id = 0
        self.title = ""
        self.body = ""
        self.userId = 0
    }
}
extension PostModel: Mappable {
    mutating func mapping(map: Map) {
        id       <- map["id"]
        title     <- map["title"]
        body     <- map["body"]
        userId     <- map["userId"]
    }
}

 
     
     
     
     
    