Jestem tak zdezorientowany co do tablic 2D w Swift. Opiszę krok po kroku. Czy mógłbyś mnie poprawić, jeśli się mylę.
Po pierwsze; deklaracja pustej tablicy:
class test{
var my2Darr = Int[][]()
}
Po drugie wypełnij tablicę. (np. my2Darr[i][j] = 0gdzie i, j są zmiennymi pętli for)
class test {
var my2Darr = Int[][]()
init() {
for(var i:Int=0;i<10;i++) {
for(var j:Int=0;j<10;j++) {
my2Darr[i][j]=18 /* Is this correct? */
}
}
}
}
I na koniec Edycja elementu in array
class test {
var my2Darr = Int[][]()
init() {
.... //same as up code
}
func edit(number:Int,index:Int){
my2Darr[index][index] = number
// Is this correct? and What if index is bigger
// than i or j... Can we control that like
if (my2Darr[i][j] == nil) { ... } */
}
}
var my2DArray = Array(count: 10, repeatedValue: Array(count: 10, repeatedValue: 18))Naprawdę powinieneś uaktualnić do nowszej wersji beta. Int[][]()nie jest już prawidłową składnią. Zostało zmienione na [[Int]]().