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! 馃槃
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(){}.
Most helpful comment
You can create a
Mapobject 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.