本文介绍了为什么此数组超出索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将元素按这种方式构建到二维数组中,但是当代码进入2D数组时,我得到一个错误:数组超出索引。
I am trying to sort elements into a two dimensional array I built this way, but when the code gets to the 2D array I get an error: "array out of index".
var categoryTempArray: [[ProductCatalogue]] = [] func sortinOut(){ var i = 0 var j = 0 for x in categories{ for y in array{ if x == String(stringInterpolationSegment: y.categoryName){ categoryTempArray[i].append(y) //categoryTempArray[i][j] = y tried this too j++ } } i++ }}推荐答案
您应该先初始化第[i]个元素,然后再附加一些[j]
You should initialize the [i]th element before than appending some [j]th element to it.
您将在不存在的项目上调用append()函数,因为categoryTempArray数组中不存在任何项目,因此索引的边界。
You would call the append() function on an item that does not exist, since no item exists in the categoryTempArray array, thus the index out of bound.
更多推荐
为什么此数组超出索引?
发布评论