我有以下代码:
var TabActions: Dictionary<String, Array<Dictionary<String, String>>> = [:]
TabActions = ["EVENT1" : ["TARGET1" : "ACTION1"], ["TARGET2" : "ACTION2"]]
我想把[“TARGET3”:“ACTION3”]添加到列表中,但我不知道该怎么做。我试过:
TabActions["EVENT1"] = [["TARGET3" : "ACTION3"]]
但是它替换了值而不是添加它,所有其他尝试都以错误告终
最好的语法是什么?
最佳答案
如果你真的需要一个字典数组,那么安东尼奥的答案是正确的,append将完成以下工作:
var TabActions: Dictionary<String, Array<Dictionary<String, String>>> = [:]
TabActions = ["EVENT1" : [["TARGET1" : "ACTION1"], ["TARGET2" : "ACTION2"]]]
TabActions["EVENT1"]?.append(["TARGET3" : "ACTION3"])
另一方面,如果你能用一本更简单的字典,你只需要做:
var TabActions: Dictionary<String, Dictionary<String, String>> = [:]
TabActions = ["EVENT1" : ["TARGET1" : "ACTION1", "TARGET2" : "ACTION2"]]
TabActions["EVENT1"]?["TARGET3"] = "ACTION3"
关于arrays - 快速添加嵌套在字典中的字典数组中的元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28137309/