ios - How to use inheritance with Swift and ObjectMapper? -


i'm new swift , have serialize object structure json string in ios( ios >= 8) project. decided use objectmapper library (i integrated cocoapod).

but problem structure following.

objecta can have list of children objects array stored in instance variable. objects can stored in instance array can of multiple types, objectb , objectc. so, in java gson have created interfaced , made both of classes implement , made array in objecta store interfaced types, can't figure how swift object model results in empty {} json objects.

the resulting json should this.

{"children":[   {"type":"objectb", "value1":"foo"},   {"type":"objectc", "value1":"bar", "value2":"baz"} ]} 

and

{"children":[   {},   {} ]} 

notice 2 entries have serialized objecta , objectc should have different structures.

i tried multiple things each times i'm stuck in dead end. tried using generics, tried use mappable protocol array type, tried classic class inheritence 1 failed.

have idea how can achieve ?

note know add serialization function each object , retrieve strings recursively , concatenate them. want avoid implementing json serializer myself use on alamofire used in project). implementing new serializer error prone , not clean way solve problem imo.

thanks lot.

i never had solution workaround made objects produce dictionnary values. recursively add child objects dictionnaries current dictionnary values. adds todict() function each object forced protocol. when resulting object made of tree of dictionnaries, objectmapper serialization works fine...


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -