2

Hi i am using ObjectMapper for my Json mapping and i have a model class named People which has one property personName and i am doing the mapping like this

class People : Mappable,BaseMappable{
    var personName : String?
    required init(map:Map){ }
    func mapping(map: Map){
        personName <- map["name"]
    }
}

but my problem is that i am reusing this model for more then one websservice and personName might come for different keys. In my current example i am mapping personName using name key but in my other webservice response personName should get initialised using userName key so how do i manage one variable managing for multiple keys?

Jitendra Modi
  • 2,344
  • 12
  • 34
nishith Singh
  • 2,968
  • 1
  • 15
  • 25

1 Answers1

4

I've encountered this problem many times, and there is no elegant solution.

Best solution I've found is to just change the API to keep everything consistent, but failing that, one method is to set the variable twice:

personName <- map["name"]
if personName == nil {
    personName <- map["personName"]
}
Tendai Moffatt
  • 236
  • 2
  • 4