Reputation: 588
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++
}}
Upvotes: 0
Views: 98
Reputation: 588
This worked in the end
for x in categories{
for y in array{
if x == String(stringInterpolationSegment: y.categoryName){
var tempArray: [ProductCatalogue] = [y]
categoryTempArray.append(tempArray)
j++
}
}
i++
}
then i can use array like this categoryTempArray[i][j]
Upvotes: 0
Reputation: 2073
You should initialize the [i]th element before than appending some [j]th element to it.
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.
Upvotes: 2