iosobjective-cgithub-mantle

Mantle iOS: How to create class model with variable key


I have a JSON dictionary that looks like this:

{
"111": {"dates"   : [<array_of_dates>], 
        "members" : [<array_of_members>]}, 
"112": {"dates"   : [<array_of_dates>], 
        "members" : [<array_of_members>]},
"113": {"dates"   : [<array_of_dates>], 
        "members" : [<array_of_members>]}
}

As you can see, the key for the nested dictionary is variable (111, 112, 113).

So far, every Mantle model I have made has fixed keys so I don't know how to handle this particular situation.

Any ideas?


Solution

  • you have several options:

    Once you have a value you can pass use in +JSONKeyPathsByPropertyKey, you can write a custom value transformer to process the model.

    Here are two examples to get you started:

    The most important part is to figure out how do you want to use the map you're getting from JSON. Maybe you're better off by keeping it as a dictionary in a private property and write public methods accessing it in a way that makes sense