Swift-嵌套字典

时间:2018-11-30 05:35:28

标签: arrays swift xcode dictionary nested

我需要在代码中重新创建下面的嵌套字典,但是即使我发现了很多关于该主题的问题,我也被困住了。

enter image description here

这是我需要重新创建的词典,但是我卡在了“动作”字符串上。

这就是我所做的 enter image description here

这是我的字典

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

这就是我更新测试值的方式,“ marker”是我的类,用于存储按钮操作

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

我对如何将“操作”设置为字符串和“按钮的数组链接”感到有些困惑

任何帮助都将非常感谢。

2 个答案:

答案 0 :(得分:1)

我认为字典结构应该是

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)

答案 1 :(得分: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": ""
            ]
        ]
    ]
]
相关问题