想不出一个多维数组找到这个问题的答案。如何插入值到在一个循环中迅速
我试图将值插入一多维数组,像这样
_ = 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
}
我counter
是在这一个之外的另一个循环。事情是我收到一个错误Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
,并在游乐场玩耍后,我意识到这是因为我的monthList: [[String]] = [[]]
,因此没有任何数组插入或类似的东西。如果我要给它一些初始化像monthList = [["1"], ["2"]]
然后我可以插入,直到monthList[1]
之前,它给出了一个错误monthList[2]
帮助吗?感谢
显示您的代码并提供错误的详细信息。 – matt
参见:[多维数组在斯威夫特(http://stackoverflow.com/q/24051490/1445366) –
我增加了更多的细节@马特 – Chris