Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-在数组中插入数组_Swift - Fatal编程技术网

Swift-在数组中插入数组

Swift-在数组中插入数组,swift,Swift,我遇到了一个问题,找不到解决办法。 我有一个数组,其中存储了不同对象的开始x和结束x位置 placedArray : [[String: [Int: [String: String]]]] placedArray = [["auto": [0: ["x-start": "300", "x-end": "400"]]], ["bus": [0: ["x-start": "0", "x-end": "300"]]]] 我想在列表中再添加一个自动,因此我尝试做的是: placedArray["au

我遇到了一个问题,找不到解决办法。 我有一个
数组
,其中存储了不同对象的开始x和结束x位置

placedArray : [[String: [Int: [String: String]]]]

placedArray = [["auto": [0: ["x-start": "300", "x-end": "400"]]], ["bus": [0: ["x-start": "0", "x-end": "300"]]]]
我想在列表中再添加一个自动,因此我尝试做的是:

placedArray["auto"].append([1: ["x-start": "400", "x-end": "500"]])
错误:

无法为“[[String:[Int:[String:String]]]]”类型的值下标“String”类型的索引

我想在最后

placedArray = [["auto": [0: ["x-start": "300", "x-end": "400"], 1: ["x-start": "300", "x-end": "400"]]], ["bus": [0: ["x-start": "0", "x-end": "300"]]]]
两个问题:

  • 外部对象是一个数组,因此必须用
    Int
    index(这是错误消息的语句)为其下标
  • key
    auto
    的值是一个字典,因此您不能
    追加
    内容
您必须获取
placedArray
的第一项,即
自动
条目,然后设置键
1
的值

var placedArray : [[String: [Int: [String: String]]]]
placedArray = [["auto": [0: ["x-start": "300", "x-end": "400"]]], ["bus": [0: ["x-start": "0", "x-end": "300"]]]]
placedArray[0]["auto"]?[1] = ["x-start": "400", "x-end": "500"]
print(placedArray)

要追加的行有一个字典数组。数组只能用整数下标,您用字符串下标了它。无论如何,是时候定义一个
struct
class
了,而不是把所有的东西都塞进字典里