Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Ios 如何在循环中向多维数组中插入值_Ios_Arrays_Swift - Fatal编程技术网

Ios 如何在循环中向多维数组中插入值

Ios 如何在循环中向多维数组中插入值,ios,arrays,swift,Ios,Arrays,Swift,我找不到这个问题的答案 我试图用类似这样的方法将值插入多维数组 _ = transportRef.observeEventType(.ChildAdded, withBlock: { (snapshotOne) in self.yearList.insert(snapshotOne.key, atIndex: 0) //print("snapshotone.key " + snapshotOne.key) // prints year if

我找不到这个问题的答案

我试图用类似这样的方法将值插入多维数组

_ = transportRef.observeEventType(.ChildAdded, withBlock: { (snapshotOne) in

        self.yearList.insert(snapshotOne.key, atIndex: 0)

        //print("snapshotone.key " + snapshotOne.key) // prints year

        if let snapshotTwo = snapshotOne.children.allObjects as? [FIRDataSnapshot] {

            for itemTwo in snapshotTwo {

                self.monthList[self.counter].insert(itemTwo.key, atIndex: 0)
            }
         }

         counter += 1
}
我的
计数器
位于此循环之外的另一个循环中。问题是我收到一条erro
线程1:EXC\u BAD\u指令(code=EXC\u 1386\u INVOP,subcode=0x0)
在操场上玩过之后,我意识到这是因为我的
月列表:[[String]=[[]]
,因此没有任何数组可以插入或类似的东西。如果我给它一些init,比如
monthList=[[[1],[[2]]
,那么我就能够在它给出
monthList[2]
错误之前插入到
monthList[1]


帮忙?谢谢

在尝试添加到数组之前,您可能需要在第二维度中初始化每个数组,我不知道您是否真的在这样做,因为提供的代码片段非常小。另外,我认为swift中的数组是可变的,所以您可以这样做:

monthList[counter] = [String]()

for item in itemsOne { 
    monthList[counter].append(item.key) 
}

通过使用append,您可以动态地将元素插入数组。否则,您将需要另一个计数器变量为insert方法的atIndex参数递增,因为在代码中,insert方法中的atIndex始终在索引0处插入。

在尝试添加到该数组之前,您可能需要初始化第二维度中的每个数组,我不知道您是否真的在这样做,因为提供的代码片段非常小。另外,我认为swift中的数组是可变的,所以您可以这样做:

monthList[counter] = [String]()

for item in itemsOne { 
    monthList[counter].append(item.key) 
}

通过使用append,您可以动态地将元素插入数组。否则,您将需要另一个计数器变量为insert方法的atIndex参数递增,因为在代码中,insert方法中的atIndex始终在索引0处插入。

当您声明数组数组时,外部数组为空。为了将值插入多维数组,需要创建内部数组并将其添加到外部数组

您可以通过添加一个
while
循环来完成此操作,该循环将添加更多的内部数组,直到您有足够的内存插入到:

for item in itemsOne {
    while counter >= monthList.count {
        monthList.append([String]())
    }
    monthList[counter].insert(item.key, atIndex: 0)
}
例如,如果您提前知道
计数器
0…29
中的值,您可以创建如下内部数组:

var monthList = [[String]](count:30, repeatedValue:[])

声明数组数组时,外部数组为空。为了将值插入多维数组,需要创建内部数组并将其添加到外部数组

您可以通过添加一个
while
循环来完成此操作,该循环将添加更多的内部数组,直到您有足够的内存插入到:

for item in itemsOne {
    while counter >= monthList.count {
        monthList.append([String]())
    }
    monthList[counter].insert(item.key, atIndex: 0)
}
例如,如果您提前知道
计数器
0…29
中的值,您可以创建如下内部数组:

var monthList = [[String]](count:30, repeatedValue:[])

显示您的代码并提供错误的详细信息。另请参见:我添加了更多详细信息@matt显示您的代码并提供错误的详细信息。另请参见:我添加了更多详细信息@matt非常感谢!非常感谢你!谢谢你的回答!有道理。但执行应该是这样的vacawama@Chris是的,vacuwama回答是一个比我更好的解决方案,所以不用担心。我不太了解swift或它的语法,我只是注意到在没有首先初始化数组的情况下尝试在二维数组中添加元素可能会出现逻辑错误,这是大多数编程语言都会发生的错误。vacuwama回答是一个更好的回答,可以让其他人看到:)谢谢你的回答!有道理。但执行应该是这样的vacawama@Chris是的,vacuwama回答是一个比我更好的解决方案,所以不用担心。我不太了解swift或它的语法,我只是注意到在没有首先初始化数组的情况下尝试在二维数组中添加元素可能会出现逻辑错误,这是大多数编程语言都会发生的错误。vacuwama答案是一个更好的答案,可以让其他人看到:)