Objectmapper: Explain purpose of `init?(_ map: Map)` in README

Created on 5 Jan 2016  路  6Comments  路  Source: tristanhimmelman/ObjectMapper

Hi, I'm struggling to understand what is the purpose of the method init?(_ map: Map) declared on Mappable. All examples on README have an empty implementation. Can you please clarify?

enhancement

Most helpful comment

This should definitely be noted in the documentation (README.md)!

All 6 comments

The init method is necessary so that ObjectMapper can create an instance of the object it is trying to map. It also allows you to do some validation before mapping. If a certain key that you need doesn't exist you can return nil from init?(_ map: Map) and the mapping will not happen.

This should definitely be noted in the documentation (README.md)!

Out of interest why is there an init and a mapping function? Can the failable initialiser not be used for both purposes?

@eoinoconnell The mapping could technically occur within the init function, but if we took that approach we would lose the ability write JSON and to map JSON to an existing object.

OK cool, valid point. Thanks!

The readme has now been updated with the information above

Was this page helpful?
0 / 5 - 0 ratings