Swift 2.0: How to declare nested dictionaries?

I am trying to make a complex Swift Dictionary declaration similar to a JSON package. However, the compiler fights a tooth and a nail with me. Here is what I am trying to do:

var icons: [[Any:Any]] = [
    "categories:":[
        ["cat2" : [
            ["name":"ImageName","type":"image"],
            ["name":"ImageName","type":"scroll","panel":panel1],
            ["name":"ImageName","type":"chooser","panel":[
                ["name":"ImageName"]]]
            ]
        ],
        ["cat2" : ["name":"ImageName"]],
        ["cat3" : ["name":"ImageName"]],
        ["cat4" : ["name":"ImageName"]],
        ["cat5" : ["name":"ImageName"]]
    ],
    "size":["x":"128","y":"128"]
]

I am not sure how to announce this. As you can tell, I have the first element as a String and the following auxiliary elements as additional dictionaries.

I want to know the best method for these data structures in Swift.

Thanks in advance.

+4
source share
2 answers

The correct way is NOT to use a dictionary.

You need to create several types of models (preferably structs) and combine them.

.

+4

, : [Any:[Any:Any]] ( , ) [String:[String:Any]] , categories value - , . [String:Any]

, @appzYourLife .

+1

Source: https://habr.com/ru/post/1629629/


All Articles