tristanhimmelman/AlamofireObjectMapper

Handle array with multiple types

Closed this issue · 1 comments

My JSON response (PagingModel) is an object which hold rows that can be different types

class PagingModel<T : Mappable>: Mappable {
    var fullyFetched        : Int?
    var pagingState         : String?
    var rows                : [T]?
    
    required init?(map: Map) {}
    
    func mapping(map: Map) {
        fullyFetched    <- map["fullyFetched"]
        pagingState     <- map["pagingState"]
        rows            <- map["rows"]
    }
}

How can I map the response?

Alamofire.request(url).responseObject { (response: DataResponse<PagingModel<SpecificType>>) in 

requires a specific type, but in my case it could be multiple types.

This is a question for the ObjectMapper project. See here https://github.com/Hearst-DD/ObjectMapper#staticmappable-protocol

You can use the static mappable protocol to allow you to dynamically switch between sub classes during mapping
https://github.com/Hearst-DD/ObjectMapper/blob/master/Tests/ObjectMapperTests/ClassClusterTests.swift#L67