1

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.

STerrier
  • 3,755
  • 1
  • 16
  • 41

2 Answers2

1

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)
mouseymaniac
  • 259
  • 2
  • 9
  • Thanks! This works in playground but how would I encode and decode it so I can save in a plist? I am getting the following error 'Type 'Marker' does not conform to protocol 'Decodable'' the erro is caused by the 'any' in the dictionary – STerrier Dec 02 '18 at 22:49
  • I found this article https://medium.com/@scotthoyt/swift-4-bridging-codable-json-and-string-any-1b76b9df2b2e – STerrier Dec 02 '18 at 23:25
0
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": ""
            ]
        ]
    ]
]
Nandin Borjigin
  • 2,094
  • 1
  • 16
  • 37
  • Hi Nandiin, Could you explain this a bit further please. Thanks! – STerrier Dec 04 '18 at 03:31
  • Sure. As far as I understand, your problem is to allow two, or more maybe, different types in a dictionary's "value" field. In this case, they are `String` for the `action` key and `String[]` for the `array linked of buttons` key. To achieve that, I declared an empty protocol, called `ButtonActionDictValue`, and then extended `String` and `String[]` to conform to that protocol. Then the expected dictionary would be of type `[String: ButtonActionDictValue]`. You can further refer to my another answer to a similar problem (posted below). And I hope I've got the point of your problem correctly. – Nandin Borjigin Dec 04 '18 at 09:33
  • https://stackoverflow.com/questions/43061928/how-to-create-an-array-or-dictionary-whose-values-can-only-be-string-int-and-bo/43062087#43062087 – Nandin Borjigin Dec 04 '18 at 09:33