STerrier
STerrier

Reputation: 4015

Swift - Nested Dictionaries

I need to recreate the nested dictionary below in code but I am stuck even though I found many questions on the topic.

enter image description here

This is the Dictionary I need to recreate but I am stuck on the "action" string.

This is what I have made enter image description here

This is my dictionary

var buttonactions: [String:[[String:[String:String]]]] = [:]

and this is how I update the value for testing and "marker" is my class which stores my button actions

  marker.buttonactions.updateValue([["Action" : ["array linked of buttons" : "actionKey"]]], forKey: "button actions array")

I am slightly confused how to set up the "action" as a string and "array linked of buttons"

Any help would be great thanks.

Upvotes: 1

Views: 750

Answers (2)

mouseymaniac
mouseymaniac

Reputation: 269

I think the dictionary structure should be

var buttonActions : [String: [String: [String:Any]]] = [:]
let array_linked_of_buttons = ["linked button UU":"22308345y1p245", "linked button cat...":"", "linked button":"ATT TRANS"]
let item0Dict: [String:Any] = ["action": "ON_DOWN_SET_UP", "array linked of buttons":array_linked_of_buttons]
let button_actions_array = ["button action array" : item0Dict]
buttonActions.updateValue(button_actions_array, forKey: "button actions")

print(buttonActions)

Upvotes: 1

Nandin Borjigin
Nandin Borjigin

Reputation: 2154

protocol ButtonActionDictValue {}

extension String: ButtonActionDictValue {}

extension Array: ButtonActionDictValue where Element == [String: String] {}

typealias ButtonAction = [String: ButtonActionDictValue]

let buttonActions: [ButtonAction] = [
    [ "action": "ON_DOWN_SET_UP"
    , "array linked of buttons": [
            [ "linked button UU": "22307"
            , "linked button cat": ""
            ]
        ]
    ]
]

Upvotes: 0

Related Questions