Objectmapper: [Question] Create empty object (without map)

Created on 1 Sep 2016  路  2Comments  路  Source: tristanhimmelman/ObjectMapper

First of all, great library! I've been using it in a lot of projects.

Besides using it for mapping, I also want to use it the other way around: create a new instance, fill out the properties and then convert it to a JSON string to send to the server.

Required init? requires a map, but I can't seem to create an empty one, and it's not nullable.

As a workaround, I currently do the following:

let comment = Mapper<Comment>().map("{}")
comment?.body = "Hello world!"

let json = Mapper().toJSONString(comment!)

Is there an easier/cleaner way to do this?
I've tried searching for this, but I couldn't find anything.

Thanks in advance and keep up the good work! 馃槃

Most helpful comment

You can create a Map object as follows:
let map = Map(mappingType: .FromJSON, JSONDictionary: [:])

Otherwise, you can just add the init(){} function to your model object and initialize it the standard way.

Hope this helps.

All 2 comments

You can create a Map object as follows:
let map = Map(mappingType: .FromJSON, JSONDictionary: [:])

Otherwise, you can just add the init(){} function to your model object and initialize it the standard way.

Hope this helps.

Thanks, @tristanhimmelman! I didn't figure it was that simple as just adding init(){}.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pcompassion picture pcompassion  路  3Comments

AashishSapkota picture AashishSapkota  路  3Comments

amg1976 picture amg1976  路  3Comments

nearspears picture nearspears  路  4Comments

liltimtim picture liltimtim  路  3Comments